JAVA IO - 请澄清

时间:2012-07-31 22:05:16

标签: java file io filereader filewriter

我是新手,我真的想学习这个概念,而不仅仅是复制和粘贴代码。我想学习如何使用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 个答案:

答案 0 :(得分:3)

您的代码主观上存在风格问题。我会把你的例子放在这里,好像我自己写的那样。

示例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很好。如果需要将大量原始类型写入二进制文件,可以使用类似的东西。其他例子更适合阅读文本。

既然你是新手,我也会给你一些随意的建议:

  • 不要因为它的缺点而责备一种语言,要学会克服它们。 (适用于所有语言)
  • 线程不是解决所有问题的方法。如果您需要多个,请自行编写调度程序。
  • 通常有很多方法可以做同样的事情,但通常一个方法比其他方式更正确。
  • 现在不要写代码,因为以后你会讨厌自己。
  • 您选择使用哪种风格无关紧要,但坚持下去。那就是说,我不喜欢你的。 :)
  • 无论您选择哪种款式,请确保您可以在任何条件下轻松阅读。
  • 您的计算机没有无限的内存,请不要编写假装它的代码。所有其他资源也是如此;没人喜欢馋嘴。

答案 1 :(得分:1)

我不太确定你的问题的格式会得到有益的答案。

我可以说:

“在你的第一个例子中:

BufferedWriter bw= new BufferedWriter (new FileWriter("a.txt", true)); 
String x;
while ((x=bw.readLine())!=null) {
    bw.newLine();
    bw.write(x);
    bw.flush();
}

bw.readLine()不正确。 BufferedWriter是一个编写器类 - 它不包含readLine()方法。您应该使用BufferedReader从文件中读取。

除了公然错误的代码缩进之外,其他示例都是“正确的”。

但是说这不会真正帮助你从大局出发。你写这个问题的方式让我相信你已经开始学习java并且可能是一般的编程新手:那很好。但是,我不认为这很好:

...[I] was confused and disappointed to see so many different versions of code... So I made my own

这不好。虽然看到这么多不同的方法来实现一个目标可能会让人难以理解,但是编程没有“正确”的做法(在大多数情况下......不需要与初学者争论语义)。 Java API为您提供了某些的方法和工具。在初学者编程的范围内,你不应该担心在使用方法本身时“正确”是什么 - 你应该只是熟悉自己熟悉方法并理解这些方法做了一些事情,我可以在逻辑上将它们联系起来以实现目标。

你可以查找“如何写入文件”并获得数百个彼此不同的示例 - 当另一个人搜索“如何从文件中读取”时会发生什么,并偶然发现这个StackOverflow问题和读你的笔记?我们可以把你的笔记放在“从文件中读取的另一个稍微不同的代码版本。”

通常情况下,我会说有一些关于如何做这样的事情的笔记很好 - 如果你正在参加编程课程并且会逐字测试。在你的情况下,我会劝阻它。我觉得(关键字感觉 ......我可能错了)你要编译笔记,开始编程,说“我需要从文件中读取内容,我该怎么做呢? ?”然后复制/粘贴笔记中的代码。那不会教你任何东西。你需要思考“我知道BufferedReader类可以用来读取文件 - 让我看一下javadocs,并考虑一种逻辑方法来应用它的方法来读取这个特定情况下的文件。”

JavaDocs是你的朋友。如果您不使用“笔记”并且只是编写练习程序以熟悉使用API​​,您将学得更快。我认为这样的“笔记”对于实际成为任何语言的熟练程序员都会适得其反。

很抱歉,如果我完全不以我的方式回答。只是看着你。也许逐字记忆笔记是你的学习方式。那也很酷。请记住,当你说I would appreciate if you could confirm whether [this code is] correct.“正确”没有实际意义时。代码有效。知道代码的工作原理(或者学习如何自己运行代码以查看发生的情况并在不起作用的情况下修复代码......)更为重要。

祝你好运!

相关问题