如何在一个文本文件中存储多个用户名

huangapple go评论72阅读模式
英文:

How to store multiple usernames in one textfile

问题

import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class Userfile {

    private static String name;
    private static Scanner scanage;
    private static Scanner scan;

    public static void main(String[] args) throws IOException {
        Random r = new Random();
        int age;
        String username;
        char randomc = 2;
        System.out.println("Please enter your name");

        scan = new Scanner(System.in);
        name = scan.nextLine();

        System.out.println("Please enter your age");
        scanage = new Scanner(System.in);
        age = scanage.nextInt();
        username = name.toLowerCase() + age;

        String alphabet = "xy!&69£zHDErnABCabcFfGghIiJjKkLlMmNPp";
        for (int i = 0; i < 1; i++) {
            randomc = alphabet.charAt(r.nextInt(alphabet.length()));
            String userId = username + randomc;
            System.out.println("Your Username is " + userId);
        }

        FileWriter userid = new FileWriter("file path");

        String userId = username + randomc;
        userid.write("\n" + "Username: " + userId);
        userid.close();
    }
}
英文:

My program asks the user for their name and age. The username is their name and age plus a random character at the end of it. I want to store that username into a new text file but whenever I run the code it only writes one username at a time it doesn't include the one that I added before. Here is my code Please help me I am new.

import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class Userfile {

private static String name;
private static Scanner scanage;
private static Scanner scan;

public static void main(String[] args) throws IOException {
	Random r = new Random();
	int age;
	String username;
	char randomc = 2;
System.out.println(&quot;Please enter your name&quot;);

 scan = new Scanner(System.in);
 name = scan.nextLine();
 
 System.out.println(&quot;Please enter your age&quot;);
 scanage = new Scanner(System.in);
 age = scanage.nextInt();
 username = name.toLowerCase()+age;
 


     String alphabet = &quot;xy!&amp;69&#163;zHDErnABCabcFfGghIiJjKkLlMmNPp&quot;;
     for (int i = 0; i &lt; 1; i++) {
     randomc = alphabet.charAt(r.nextInt(alphabet.length()));
     String userId = username + randomc;
     System.out.println(&quot;Your Username is &quot; +userId);
    }
       


  FileWriter userid = new FileWriter(&quot;file path&quot;);

  String userId = username + randomc;
  userid.write(&quot;\n&quot; + &quot;Username: &quot; + userId);
  userid.close();
 





  }
  }

答案1

得分: 0

如果您想要向一个之前已经写入过文本的文件中写入文本,您需要使用FileWriter(File file, boolean append)构造函数:

FileWriter userid = new FileWriter("文件路径", true); //假设"文件路径"是实际文件的路径

此外,您的程序仅会询问一次输入,所以如果您想要添加多个用户名,就需要多次运行它。或者您可以将您已完成的部分放入循环中,以多次询问输入。说到循环,您目前使用的循环没有实际意义,因为其中的语句只会执行一次,就像没有循环包裹它们时一样。

英文:

If you want to write text to a file to which you've already written text before, you need to use the FileWriter(File file,boolean append) constructor:

FileWriter userid = new FileWriter(&quot;file path&quot;, true); //assuming &quot;file path&quot; is the actual path to the file

Besides that, your program only asks for input once, so you'll need to run it multiple times if you want to add multiple usernames. Or you could wrap what you've done in a loop to ask for input multiple times. And speaking of loops, the one loop you do have serves no real purpose as the statements it executes will run once, just like they would without a loop wrapping them.

答案2

得分: 0

因为你每次都在覆盖文件。

FileWriter userid = new FileWriter("文件路径");

替换为

FileWriter userid = new FileWriter("文件路径", true);
英文:

It's because you are overriding the file everytime.

Replace

FileWriter userid = new FileWriter(&quot;file path&quot;);

with

FileWriter userid = new FileWriter(&quot;file path&quot;, true);

huangapple
  • 本文由 发表于 2020年9月13日 04:02:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63864435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定