使用扫描仪类读取txt文件并输出2个txt文件

问题描述

这是提示我 “写一个程序,并使用附件文件(babynames.txt)作为输入文件,并创建两个输出磁贴。一个文件列出所有男孩的名字,另一个文件列出所有男孩的名字”

这是我到目前为止的

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;


public class BabyNames
{
public static void main(String[] args) throws FileNotFoundException
{
  // Prompt for the input and output file names

  Scanner console = new Scanner(system.in);
  System.out.println("Input file: ");
  String inputFileName = console.next();
  System.out.println("Output file 1: ");
  String outputBoysNames = console.next();
  System.out.println("Output file 2: ");
  String outputGirlsNames = console.next();

  // Construct the Scanner and PrintWriter objects for reading and writing

  File inputFile = new File(inputFileName); // creates variable that reads from txt
  Scanner in = new Scanner(inputFile);
  PrintWriter out = new PrintWriter(outputBoysNames); // writes to output file (variable name)
  PrintWriter out2 = new PrintWriter(outputGirlsNames);

  // Read the input and write the output

  while (in.hasNextLine()) {
     String A = in.next();
     String B = in.next();
     String C = in.next();
     String D = in.next();
     String E = in.next();
     String F = in.next();
     String G = in.next();

     out.printf(B);
     out2.print(E);
  }

  in.close(); // Prevents this file is being used error
  out.close();
  out2.close();
 }
}

我假设我需要将文本文件中的数据输入到数组中。每个令牌用空格分隔。我不知道该如何处理。另外,在建立数组之后,我需要将out.printf()out2.printf()更改为if循环,以将所需的标记放入其各自的txt文件中。

输入txt文件中的示例行

1 Michael 462085 2.2506 Jessica 302962 1.5436

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)