我需要一个程序,它会要求用户输入要保存的信息,逐行保存在文件中。我该怎么做?

我需要一个程序,它会要求用户输入要保存的信息,逐行保存在文件中。我该怎么做?


它必须看起来像这样:


Please, choose an option:


1. Read a file


2. Write in a new file


2


File name? problema.txt


How many lines do you want to write? 2



Write line 1: Hey


Write line 2: How are you?


Done! The file problema.txt has been created and updated with the content given. 

我尝试过各种方法,但没有成功。首先,我在一个二维数组中完成了它,但我不能跳到下一行。


然后我尝试使用没有数组的“.newline”方法,但它不能让我保存一个以上的单词。


尝试 1


System.out.println("How many lines do you want to write? ");

int mida = sc.nextInt();



PrintStream escriptor = new PrintStream(f);

String [][] dades = new String [mida][3];


for (int i = 0; i < dades.length; i++) {


  System.out.println("Write line " + i + " :");

  for (int y=0; y < dades[i].length; y++) {


    String paraula = sc.next();

    System.out.println(paraula + " " + y);


    dades[i][y] = paraula;


    escriptor.print(" " + dades[i][y]);



  }

  escriptor.println();

}

尝试 2


System.out.println("How many lines do you want to write? ");

int mida = sc.nextInt();


PrintStream escriptor = new PrintStream(f);

BufferedWriter ficheroSalida = new BufferedWriter(new FileWriter(new File(file1)));


for (int i = 0; i < mida; i++) {


  System.out.println("Write line " + i + " :");

  String paraula = sc.next();

  ficheroSalida.write (paraula);

  ficheroSalida.newLine();

  ficheroSalida.flush();


}


System.out.println("Done! The file " + fitxer + " has been created and updated with the content given. ");


escriptor.close();

尝试 1:


Write line 1: Hey How are


Write line 1: you...

尝试 2:


Write line 1: Hey


Write line 2: How


Write line 3: are


Write line 4: you


Write line 5: ?


慕少森
浏览 97回答 2
2回答

胡说叔叔

好吧,你快到了。首先,我会使用 ajava.io.FileWriter将字符串写入文件。如果您只是想将这些行写入文件,那么实际上没有必要在这里使用数组。您还应该使用try-with-resources 语句来创建您的编写器。这确保escriptor.close()即使出现错误也会被调用。在这种情况下您也不需要调用.flush(),因为这将在句柄关闭之前完成。你打算自己做这件事很好,但一般来说,尽可能使用这种特殊的声明更安全。import java.io.*;import java.util.Scanner;public class Example {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; File f = new File("/tmp/output.txt");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("How many lines do you want to write? ");&nbsp; &nbsp; &nbsp; &nbsp; int mida = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; sc.nextLine(); // Consume next empty line&nbsp; &nbsp; &nbsp; &nbsp; try (FileWriter escriptor = new FileWriter(f)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < mida; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(String.format("Write line %d:", i + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String paraula = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; escriptor.write(String.format("%s\n", paraula));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕森卡

如果您的文本文件很小并且不需要使用流阅读器/流编写器,您可以阅读文本,添加您想要的内容并重新编写。检查这个例子:public class ReadWrite {&nbsp; &nbsp; private static Scanner scanner;&nbsp; &nbsp; public static void main(String[] args) throws FileNotFoundException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; scanner = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; File desktop = new File(System.getProperty("user.home"), "Desktop");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Yo, which file would you like to edit from " + desktop.getAbsolutePath() + "?");&nbsp; &nbsp; &nbsp; &nbsp; String fileName = scanner.next();&nbsp; &nbsp; &nbsp; &nbsp; File textFile = new File(desktop, fileName);&nbsp; &nbsp; &nbsp; &nbsp; if (!textFile.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println("File " + textFile.getAbsolutePath() + " does not exist.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String fileContent = readFileContent(textFile);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("How many lines would you like to add?");&nbsp; &nbsp; &nbsp; &nbsp; int lineNumber = scanner.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= lineNumber; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Write line number #" + i + ":");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line = scanner.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileContent += line;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileContent += System.lineSeparator();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //Write all the content again&nbsp; &nbsp; &nbsp; &nbsp; try (PrintWriter out = new PrintWriter(textFile)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.write(fileContent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.flush();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; scanner.close();&nbsp; &nbsp; }&nbsp; &nbsp; private static String readFileContent(File f) throws FileNotFoundException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; try (BufferedReader br = new BufferedReader(new FileReader(f))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line = br.readLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (line != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(System.lineSeparator());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line = br.readLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String everything = sb.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return everything;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}该示例的执行将是:Yo, which file would you like to edit from C:\Users\George\Desktop?hello.txtHow many lines would you like to add?4Write line number #1:HelloWrite line number #2:StackWrite line number #3:OverWrite line number #4:Flow文件包含以下内容:HelloStackOverFlow如果您再次运行,输入以下内容:Yo, which file would you like to edit from C:\Users\George\Desktop?hello.txtHow many lines would you like to add?2Write line number #1:HeyWrite line number #2:too文本文件将包含:HelloStackOverFlowHeytoo但是,如果您尝试处理大文件,您的内存将不够,因此OutOfMemoryError将抛出 an。但是对于小文件,没问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java