java File类笔记

一、建立文件

windows环境下:

File file1 = new File("C:/test/a.txt");

File file2 = new File("C:\\test\\b.txt");

分隔符可用 File.separator


二、获取文件全部内容

Long fileLengthLong = f.length();
byte[] fileContent = new byte[fileLengthLong.intValue()];   
FileInputStream inputStream = new FileInputStream(f);
inputStream.read(fileContent);
inputStream.close();
String str = new String(fileContent);


三、按行读取文件

BufferedReader reader = new BufferedReader(new FileReader(f));
String s ;
while ((s = reader.readLine()) != null) {
    System.out.println(s);
    }


四、写入文件

FileWriter writer = new FileWriter(dstFile)
writer.write("example");
writer.close();


你可能感兴趣的:(java,windows)