Java 如何在文件i/o中读写答案

Java 如何在文件i/o中读写答案,java,file-io,Java,File Io,我有我正在处理的代码,我可以从文件中读取,但我无法将答案保存到我的txt文件中。还有,我如何回忆对同一号码执行其他操作。我需要一个如何执行的提示 package x; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class x { public static void main(String args[]) throws FileNotF

我有我正在处理的代码,我可以从文件中读取,但我无法将答案保存到我的txt文件中。还有,我如何回忆对同一号码执行其他操作。我需要一个如何执行的提示

 package x;

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



public class x {

    public static void main(String args[]) throws FileNotFoundException {

        //creating File instance to reference text file in Java
        File text = new File("C:\\Users\\user\\Desktop\\testScanner.txt");

        //Creating Scanner instnace to read File in Java
        Scanner scnr = new Scanner(text);

        //Reading each line of file using Scanner class
        int lineNumber = 1;
        while(scnr.hasNextLine()){
             String line = scnr.nextLine();
            int foo = Integer.parseInt(line);

            System.out.println("===================================");
            System.out.println("line " + lineNumber + " :" + line);
            foo=100*foo;
            lineNumber++;
            System.out.println(" foo=100*foo " + lineNumber + " :" + foo);
        }       

    }   

}

您需要使用filewriter来编写文件,使用filereader来编写文件。您还需要导入java.io。下面是一个示例代码:

import java.io.*;

public class FileRead{

   public static void main(String args[])throws IOException{

      File file = new File("Hello1.txt");
      // creates the file
      file.createNewFile();
      // creates a FileWriter Object
      FileWriter writer = new FileWriter(file); 
      // Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();

      //Creates a FileReader Object
      FileReader fr = new FileReader(file); 
      char [] a = new char[50];
      fr.read(a); // reads the content to the array
      for(char c : a)
          System.out.print(c); //prints the characters one by one
      fr.close();
   }
}

到目前为止你试过什么?请阅读以帮助你获得更好(可能更快)的答案。如果你需要提示,请仔细阅读