基础知识:
读文件:
写文件:
代码:
package day12;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Test1 {
public static void main(String[] args) {
Test1.testFileInputStream();
Test1.testFileOutputStream();
}
public static void testFileInputStream(){
try {
FileInputStream in = new FileInputStream("D:/test/1.txt");
byte[] b = new byte[10];
int len = 0;
while((len = in.read(b)) != -1){
System.out.println(new String(b,0,len));
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testFileOutputStream(){
try {
FileOutputStream out = new FileOutputStream("D:/test/output.txt");
String str = "knsasjadkajsdkjsa";
out.write(str.getBytes());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
|