我是一个新手,我真的很想学习这个概念,而不是仅仅复制和粘贴代码。我想了解如何准确使用 Jave IO,但看到我的不同版本的代码感到困惑和失望。
所以我自己做了笔记,想和这里的专家确认我是否做对了。这些仅供我自己引用。我知道并不完美,但如果您能确认它们是否正确,我将不胜感激。
使用 BufferedWriter 和 FileWriter 写入文本文件(作为字符)。缺点是您不能编写原始数据类型。
例如:
BufferedWriter bw= new BufferedWriter (new FileWriter("a.txt", true));
String x;
while ((x=bw.readLine())!=null){
bw.newLine();
bw.write(x);
bw.flush();}
使用 BufferedReader 和 FileReader 读取文本文件(作为字符)
例如:
BufferedReader br = new BufferedReader (new FileReader("b.txt"));
String x;
while ((x=br.readLine())!=null){
System.out.println(x);}
使用 DataOutputStream 和 FileOutputStream 写入文本文件(二进制)。优点是您可以编写原始数据类型以及字符串。
例如:
DataOutputStream dos = new DataOutputStream(new FileOutputStream("out.txt"));
dos.writeInt(cityIdA); // int cityIdA = 9897;
dos.writeUTF(cityNameA); // String cityNameA = "Green Lake City";
dos.writeInt(cityPopulationA); // int cityPopulationA = 500000;
dos.writeFloat(cityTempA); // float cityTempA = 15.50f;
dos.flush();
使用 DataInputStream 和 FileInputStream 读取文本文件(二进制)。优点是您可以读取原始数据类型和字符串。
例如:
DataInputStream dis = new DataInputStream(new FileInputStream("inp.txt"));
int cityId1 =dis.readInt(); // int cityIdA = 9897;
String cityName1 =dis.readUTF(); // String cityNameA = "Green Lake City";
int cityPopulation1 =dis.readInt(); // int cityPopulationA = 500000;
float cityTemperature1 =dis.readFloat(); // float cityTempA = 15.50f;
实际代码:
import java.io.*;
class b{
public static void main (String args[]) throws IOException{
int cityIdA = 9897;
String cityNameA = "Green Lake City";
int cityPopulationA = 500000;
float cityTempA = 15.50f;
BufferedWriter bw = new BufferedWriter(new FileWriter("shahar.txt"));
bw.write("9897");
bw.write("Green Lake City");
bw.write("500000");
bw.write("15.50");
bw.flush();
bw.close();
DataOutputStream dos = new DataOutputStream(new FileOutputStream("out.txt"));
dos.writeInt(cityIdA);
dos.writeUTF(cityNameA);
dos.writeInt(cityPopulationA);
dos.writeFloat(cityTempA);
dos.flush();
BufferedReader br = new BufferedReader (new FileReader("shahar.txt"));
String x;
while ((x=br.readLine())!=null){
System.out.println(x);}
DataInputStream dos1 = new DataInputStream(new FileInputStream("out.txt"));
int cityId1 = dos1.readInt(); // int cityIdA = 9897;
System.out.println( cityId1);
String cityName1 =dos1.readUTF(); // String cityNameA = "Green Lake City";
System.out.println(cityName1);
int cityPopulation1 =dos1.readInt(); // int cityPopulationA = 500000;
System.out.println(cityPopulation1);
float cityTemperature1 =dos1.readFloat(); // float cityTempA = 15.50f;
System.out.println(cityTemperature1);
}
}
最佳答案
您的代码主观上存在风格问题。我会把你的例子放在这里,就好像我自己写的一样。
例子2
(我把它放在示例 1 之前是有原因的)
BufferedReader br = new BufferedReader (new FileReader("b.txt"));
String x;
while ((x = br.readLine()) != null)
{ // changed style, but otherwise fine
System.out.println(x);
}
例子1
// borrowed reader from example 1
BufferedReader br = new BufferedReader (new FileReader("b.txt"));
BufferedWriter bw = new BufferedWriter (new FileWriter("a.txt", true));
String x;
// this part didn't work. You were trying to read from a writer, as you write to it.
// if this were an object capable of reading and writing at the same time, you would
// run into issues because they wouldn't be rewinding after each operation and you'd
// end up with a combination of unexpected mirrored lines and garbled crap.
// changed loop to read from reader. reads data from one file, line by line, and
// writes to another.
while ((x = br.readLine()) != null)
{
bw.write(x); // in the other order, file will start with a blank line
bw.newLine(); // in this order, file will end with one instead
}
bw.flush(); // its better if this is not in the loop
示例 3 和 4
示例 3 和 4 没问题。如果您需要将大量原始类型写入二进制文件,您将使用类似的东西。其他示例更适合阅读文本。
既然你是新手,我也给你一些随机的建议:
- 不要责怪语言的缺点,要学会克服它们。 (适用于所有语言)
- 线程并不能解决所有问题。如果您需要多个,请自己编写一个调度程序。
- 通常有多种方法可以做同一件事,但通常有一种方法比其他方法更正确。
- 现在不要编写代码,因为以后您会讨厌自己。
- 您选择使用哪种风格无关紧要,但请坚持使用。也就是说,我不喜欢你的。 :)
- 无论您选择哪种风格,请确保您在任何情况下都能轻松阅读。
- 您的计算机没有无限内存,不要假装它有无限内存。所有其他资源也是如此;没有人喜欢贪吃的人。
关于JAVA IO - 请澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11749697/