//FileReader文件读取流:把文件TestFileReader.java里的字符读取出来
import java.io.*;
public class TestFileReader {
public static void main(String[] args) {
FileReader fr = null;
int c = 0;
try {
fr = new FileReader("F:/javashipin/TestFileReader.java");
int ln = 0;
while ((c = fr.read()) != -1) {
//char ch = (char) fr.read();
System.out.print((char)c);
//if (++ln >= 100) { System.out.println(); ln = 0;}
}
fr.close();
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
} catch (IOException e) {
System.out.println("文件读取错误");
}
}
}
==============================================================
//FileWriter文件写入流:把数据(for循环里的数据)写入到文件(test.txt)中,里面显示的是国际上任何语言的组合,看到只是一堆乱码,不过可以转换成字符串的类型就不会了
import java.io.*;
public class TestFileWriter {
public static void main(String[] args) {
FileWriter fw = null;
String s;
try {
fw = new FileWriter("F:/javashipin/test.txt");
for(int c=0;c<=50000;c++){
s = String.valueOf(c);
fw.write(s);
//fw.write(c);
}
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("文件写入错误");
System.exit(-1);
}
}
}
/*
====================================================================================================
*/
//从FileReader文件读取流中读取的数据写入到FileWriter文件写入流中,test.txt不会产生上面的一堆乱码情况
/*import java.io.*;
public class TestFileWriter {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("F:/javashipin/TestFileWriter.java");
FileWriter fw = new FileWriter("F:/javashipin/test.txt");
int b;
//作为整数读取的字符b=fr.read(),范围在 0 到 65535 之间 (0x00-0xffff),如果已到达流的末尾,则返回 -1
while((b = fr.read()) != -1) {
fw.write(b);
}
fr.close();
fw.close();
}
}
*/