简体   繁体   English

文件读写

[英]File Read and Write

I Have a code for writing into a file each time there is an entry into the textfield. 每当文本字段中有一个条目时,我都有一个用于写入文件的代码。 but after the process, when an entry is made again the file is rewritten instead of continuing with the entries to the file, and so I lose the previous data. 但是在此过程之后,当再次输入条目时,文件将被重写,而不是继续输入文件,因此我丢失了之前的数据。 How do I rewrite to the existing one, so that later I get to read those data even after closing the app. 我如何重写现有数据,以便即使关闭应用程序后也可以读取这些数据。

The code for the write process is given : 给出了写过程的代码:

public boolean writeToFile(String dataLine) {
  dataLine = "\n" + dataLine;


try {
  File outFile = new File(filepath);

    dos = new DataOutputStream(new FileOutputStream(outFile));

  dos.writeBytes(dataLine);
  dos.close();
} catch (FileNotFoundException ex) {
  return (false);
} catch (IOException ex) {
  return (false);
}
return (true);

} }

Could anyone pls make changes to the code as necessary and post it to me. 任何人都可以根据需要对代码进行更改并将其发布给我。

The [Java API for FileOutputStream][1] states: [FileOutputStream的Java API] [1]指出:

public FileOutputStream(String name,
                        boolean append)
                throws FileNotFoundException)

Creates an output file stream to write to the file with the specified name. 创建输出文件流以写入具有指定名称的文件。 If the second argument is true, then bytes will be written to the end of the file rather than the beginning. 如果第二个参数为true,则字节将被写入文件的末尾而不是开头。 A new FileDescriptor object is created to represent this file connection. 创建一个新的FileDescriptor对象来表示此文件连接。

So, your code should look like this: 因此,您的代码应如下所示:

public boolean writeToFile(String dataLine) {
  dataLine = "\n" + dataLine;
  try {
    File outFile = new File(filepath);
    dos = new DataOutputStream(new FileOutputStream(outFile,true));
    dos.writeBytes(dataLine);
    dos.close();
  } catch (FileNotFoundException ex) {
    return (false);
  } catch (IOException ex) {
    return (false); 
  }
  return (true);
}

[1]: http://download.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.lang.String , boolean) [1]: http : //download.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.lang.String ,boolean)

Open file in append mode. append模式下打开文件。

make it 做了

dos = new DataOutputStream(new FileOutputStream(outFile),true);

将构造函数FileOutputStream(File file, boolean append)与append = true一起使用

See [http://download.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.io.File, boolean)][1] 参见[http://download.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.io.File,boolean)] [1]

Also note that you should probably use a FileWriter to write text. 另请注意,您可能应该使用FileWriter编写文本。 FileOutputStream is for binary data. FileOutputStream用于二进制数据。

[1]: http://download.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.io.File , boolean) [1]: http : //download.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.io.File ,boolean)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM