如何将数据添加到文本文件而不覆盖Java中的内容 [英] How do I add data to text file and not overwrite what I have in Java

查看:409
本文介绍了如何将数据添加到文本文件而不覆盖Java中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,这是我的代码,但是它已经覆盖了文本文件中已有的内容.我想要的是将其添加到文本文件的新行中.

This is my code so far but it overwrites what I have in the text file already. What I want is for it to add it to a new line in the text file.

import java.io.*;
import java.util.Scanner;

public class Login{
  public static void main(String[] args) throws IOException {
    Scanner s1,s2;
    s1 = new Scanner(new FileInputStream("login.txt"));
    s2 = new Scanner(System.in);
    boolean loggedIn = false;
    String name,pword,n,p;
    System.out.println("Are you a new user? (Type y for yes or n for no)");
    String nU = s2.next();

    if (nU.equals("n"))
    {
    System.out.println("Enter username:");
    n=s2.next();
    System.out.println("Enter password:");
    p=s2.next();
    while(s1.hasNext()){
      name=s1.next();
      pword=s1.next();
      if(n.equals(name) && p.equals(pword)){
        System.out.println("You are logged in.");
        loggedIn = true;
        break;
      }
    }
    if(!loggedIn)
      System.out.println("Incorrect password or username.");
    }
    else if (nU.equals("y"))
    {

下面是我的代码的问题所在,因为这是将其写入文件的地方.

Down here is where the problem with my code will be as this is where it is writing it to the file.

     PrintWriter out = new PrintWriter("login.txt"); 
     System.out.println("Enter username:");
     n=s2.next();
     System.out.println("Enter password:");
     p=s2.next();
     out.append(n);
     out.append(p);
     out.close();
     System.out.println("Account has been created and you are logged in.");
    }
    else
      System.out.println("Invalid response.");

推荐答案

建议使用BufferedWriterFileWriter链,关键点是FileWriter将在使用时将字符串附加到当前文件通过在最后一个参数中添加true这样的

It is advised to use chain of BufferedWriter and FileWriter, and the key point is FileWriter will append String to current file when use the one of its constructor that lets appaneding by adding true to last paramter like

new FileWriter("login.txt", true)        

以及当我们用BufferedWriter对象将其包围时,如果要多次写入文件,则效率更高,因此它会将字符串缓冲为大块并将大块写入文件中,并且很明显您可以节省大量时间来写入文件

and when we surrounding it with BufferedWriter object in order to be more efficient if you are going to write in the file number of time, so it buffers the string in big chunk and write the big chunk into a file and clearly you can save a lot of time for writing into a file

注意:可能不使用BuffredWriter,但建议使用此方法,因为它具有更好的性能和缓冲大字符串并写入一次的能力

Note :It is possible not to use BuffredWriter ,but it is advised because of better performance and ability to buffer the big chunk of Strings and write them once

只需更改您的

PrintWriter out = new PrintWriter("login.txt"); 

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("login.txt", true)));

示例:

try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("login.txt", true)));) {
    String data = "This content will append to the end of the file";
    File file = new File("login.txt");
    out.println(data);
} catch(IOException e) {
}

可以不使用BufferedWriter来解决此问题,但是性能会降低,就像我提到的那样.

It is possible to solve this issue without using BufferedWriter, yet the performance will be low as I mentioned.

示例:

try (PrintWriter out = new PrintWriter(new FileWriter("login.txt", true));) {
    String data = "This content will append to the end of the file";
    File file = new File("login.txt");
    out.println(data);
} catch (IOException e) {
}

这篇关于如何将数据添加到文本文件而不覆盖Java中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆