public static void main(String[] args) throws IOException {
String Path="E:\\Linux公社\\JAVA";
File file=new File("E:\\Linux公社\\JAVA","BaiYiShaoNian.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Java写入文件的三种方法
* */
FileOutputStream fos=null;
BufferedWriter bw=null;
FileWriter fw=null;
int value=1000;
try {
fos=new FileOutputStream(new File(Path+"fos.txt"));
long begin=System.currentTimeMillis();
for (int i=1; i<=value; i++) {
fos.write(5);
}
long end=System.currentTimeMillis();
System.out.println("TheCostTime of FileOutputStream is : " + (end-begin));
fos.close();
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(Path+"br.txt")),"UTF8"));
begin=System.currentTimeMillis();
for (int i=1; i<=value; i++) {
bw.write(5);
bw.newLine();
}
bw.close();
end=System.currentTimeMillis();
System.out.println("TheCostTime of BufferedWriter is : " + (end-begin));
fw=new FileWriter(Path+"fw.txt");
begin=System.currentTimeMillis();
for (int i=1; i<=value; i++) {
fw.write(5);
}
fw.close();
end=System.currentTimeMillis();
System.out.println("TheCostTime of FileWriter is : " + (end-begin));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fos.close(); //FileOutputStream
bw.close(); //BufferedWriter
fw.close(); //FileWriter
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Java读写文件方法总结(3)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://www.heiqu.com/02c1908e4bca3f403b2862f12644999b.html