public class ReaderAndWriter {
public static void main(String[] args) throws IOException {
File file = new File("word.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = null;
fw = new FileWriter(file,true);
fw.write("wlj love rjl\n");
System.out.println("成功写入");
fw.close();
/*FileReader fr = null;
if (file.length() == 0) {
System.out.println("文本中字符数为0!");
}
else {
fr = new FileReader(file);
char[] cubf = new char[1024];
int hasread = -1;
while ((hasread = fr.read(cubf)) != -1) {
System.out.println(new String(cubf,0,hasread));
}
System.out.println("成功读取");
fr.close();
}*/
FileReader fr = new FileReader(file);
BufferedReader bufr = new BufferedReader(fr);
String string = null;
while ((string=bufr.readLine())!=null){
String name1 = string.substring(0,3);
String s = string.substring(4,8);
String name2 = string.substring(9,12);
System.out.println(name1);
System.out.println(s);
System.out.println(name2);
System.out.println(string);
}
fr.close();
}
}
Java文件的读取写入
最新推荐文章于 2024-07-31 21:34:18 发布