Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将void方法的结果捕获到输出文件中_Java_Printstream - Fatal编程技术网

Java 将void方法的结果捕获到输出文件中

Java 将void方法的结果捕获到输出文件中,java,printstream,Java,Printstream,我是Java新手,所以谢谢你的帮助 public static int convert (String value) { int temp_convert = 0; // Setting up new Scanner to read the User-input string Scanner token = new Scanner(value); // Take out the word "CONVERT" String fnc

我是Java新手,所以谢谢你的帮助

    public static int convert (String value) {
      int temp_convert = 0;
      // Setting up new Scanner to read the User-input string
      Scanner token = new Scanner(value);
      // Take out the word "CONVERT"
      String fnc = token.next();
      // Get the temperature that needs to be converted
      int temp = token.nextInt();
      // Current unit of temperature
      String type = token.next().toLowerCase();
      if (type.equals("f")) {
         temp_convert = (int) Math.round((temp - 32)/1.8); 
         System.out.println(temp_convert + "C");
      } else if (type.equals("c")) {
         temp_convert = (int) Math.round(1.8 * temp + 32);
         System.out.println(temp_convert + "F");
      }
      return temp_convert;
   }
我正在尝试使用PrintStream将此方法的打印结果获取到输出文件中。我需要用这种方法打印的任何行都打印到输出文件中。我该怎么做?这是我到目前为止的代码,但它在.txt文件中还没有生成任何内容

public static void readFile (Scanner console, String file_input) throws FileNotFoundException {
      // Setting up new Scanner to scan the file
      Scanner input = new Scanner (file_input);
       // Prompt user for output file's name
      System.out.print("Output file name: ");
      String name_output = console.next();
      PrintStream file_output = new PrintStream(new File(name_output));
      System.out.println("YazLang program interpreted and output to .txt file!");
      System.out.println();
      while (input.hasNext()) {
         String value = input.nextLine().toLowerCase();
         if (value.startsWith("convert")) {
            int concert_temp = convert(value);
            file_output.println(concert_temp);
         } else if (value.startsWith("range")) {
            range(value);
         } else if (value.startsWith("repeat")) {
            repeat(value);
         }
      }
   } 


为什么不将
convert
方法的签名更改为return
int

public static int convert (String value) {
    Scanner token = new Scanner(value);
    // ...
    return value;
}
然后将结果写入
readFile
方法中的文件,如下所示:

public static void readFile (Scanner console, String file_input) throws FileNotFoundException {
  // omitted..
  while (input.hasNext()) {
     String value = input.nextLine().toLowerCase();
     if (value.startsWith("convert")) {
        int concert_temp = convert(value);
        file_output.println(concert_temp);
     } else if (value.startsWith("range")) {
        // here
        int concert_temp = range(value);
        file_output.println(concert_temp);
     } else if (value.startsWith("repeat")) {
        repeat(value);
     }
  }
} 
您可以使用System.setOut()() 在readFile方法的第7行之后插入此行:

System.setOut(file_output);
有两种选择:

  • 在最外层的函数中定义
    PrintStream
    ,然后将其传递给所需的任何函数。继续使用
    PrintStream::print…
    方法写入文件

  • 定义将写入两个流的包装器类:

  • 初始化打印机:

    DuplexPrinter printer = new DuplexPrinter(file_output);
    
    现在,将对
    System.out.println
    的每个调用替换为:

    printer.println()
    
  • 使用第三方库。比如说

  • void方法没有结果。也就是说,写入文件时不需要返回值。你到底有什么问题?也就是说,这个://设置新的扫描器来读取用户输入的字符串Scanner token=newscanner(value);这有点胡说八道。我已经测试了你的代码,它会按照应该的方式写入文件。可能会检查您的文件是否确实存在,或者使用调试器?因为我仍然希望在值结果旁边有F/C。什么是F/C…?温度类型(摄氏度或华氏度),因为它是一种转换方法。
    printer.println()