【问题标题】:Writing to File From Input Log从输入日志写入文件
【发布时间】:2017-06-28 16:26:14
【问题描述】:

所以我在这里遇到了一些麻烦。我需要能够将我的输出写入文件,并让它只包含代码中指定的关键字。此代码没有向文件写入任何内容,它只会打开另一个框供用户输入。我如何让它在用户输入文件名后关闭输入框,让它将输出写入文件,并让输出显示在编译器中?谢谢!

import java.util.*;
import java.io.*;

public class Classname {

   static Scanner sc = new Scanner(System.in);

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

   String filename;

   // Connecting to a file with a buffer
   PrintWriter outFile = new PrintWriter(
                          new BufferedWriter(
                           new FileWriter("chatOutput.log")));

   // Get the file
   System.out.print("Please enter full name of the file: ");
   filename = sc.next();

   // Assign the name of the text file to a file object
   File log = new File( filename);
   String textLine = null; // Null
   String outLine = "";    // Null

   while(sc.hasNext())
   {
     String line=sc.nextLine();
     if(line.contains("LANTALK"))
     System.out.println(line);
   }

   try
   {
     // assigns the file to a filereader object..this will throw an error if 
     the file does not exist or cannot be found
     BufferedReader infile = new BufferedReader(new FileReader(log));

   try
   {
     // read data from a file..this will throw and error if something goes 
     wrong reading (empty or past end of file)
     while((textLine = infile.readLine()) != null)
   {
     //System.out.printf("%s\n",textLine);
     outLine = textLine.toUpperCase();
     outFile.printf("%s\n",outLine);
   }// end of while 
   } // end of try

  finally  // finally blocks get executed even if an exception is thrown 
   {
     infile.close();
     outFile.close();
   }
   }// end of try

 catch (FileNotFoundException nf) // this goes with the first try because it 
 will throw a FileNotFound exception
   {
     System.out.println("The file \""+log+"\" was not found"); 
   }
 catch (IOException ioex) // this goes with the second try because it will 
 throw an IOexception
   {
     System.out.println("Error reading the file");
   }

   } /// end of main

 } // end of class

【问题讨论】:

  • 在寻求免费帮助时,至少要花时间正确格式化您的代码。
  • 这会让你更快乐吗?
  • 伙计们,我仍然需要帮助。

标签: java file input writing


【解决方案1】:

您需要的是结束 while(sc.hasNext()) while 循环,因为 Scanner sc 将始终有一个下一个,因为您实际上是在问自己是否从用户那里得到该行,然后等待下一行sc.nextLine();然后你把它放到一个字符串中,所以下次你问自己我有没有这条线时,答案是肯定的,无论如何克服这个问题有点复杂,你需要更改 while 循环以有一个特殊的词来阻止它,所以你必须改变它:

  while(sc.hasNext()){
     String line=sc.nextLine();
     if(line.contains("LANTALK"))
     System.out.println(line);
   }

例如:

   while(true){
     String line=sc.nextLine();
     if(line.contains("LANTALK"))
     System.out.println(line);
     if(line.contains("END"))
      break;
   }

您还需要检查用户输入的文件是否存在,并将控制台中的文本实际添加到文件中,所以它看起来像这样:

 if(!log.exists())log.createNewFile();
// Connecting to a file with a buffer
   PrintWriter logFile = new PrintWriter(
                          new BufferedWriter(
                           new FileWriter(log.getAbsolutePath())));
   while(true){
     String line=sc.nextLine();
     if(line.contains("LANTALK"))
     System.out.println(line);
     if(line.contains("END"))
      break;
     logFile.println(line);

   }

   logFile.close();

现在我们要做的就是在将输出写入 logFile 时将输出打印到控制台,所以 while((textLine = infile.readLine()) != null) 现在看起来有点像这样:

  while((textLine = infile.readLine()) != null)
   {
     //System.out.printf("%s\n",textLine);
     outLine = textLine.toUpperCase();
     outFile.println(outLine);
     System.out.println(outLine);
   }// end of while 
   } // end of try

所以最后这个洞应该看起来像这样:

import java.util.*;
import java.io.*;

public class Classname{

   static Scanner sc = new Scanner(System.in);

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

   String filename;

   // Connecting to a file with a buffer
   PrintWriter outFile = new PrintWriter(
                          new BufferedWriter(
                           new FileWriter("chatOutput.log")));

   // Get the file
   System.out.print("Please enter full name of the file: ");
   filename = sc.next();

   // Assign the name of the text file to a file object
   File log = new File(filename);
   String textLine = null; // Null
   String outLine = "";    // Null

   if(!log.exists())log.createNewFile();
// Connecting to a file with a buffer
   PrintWriter logFile = new PrintWriter(
                          new BufferedWriter(
                           new FileWriter(log.getAbsolutePath())));
   while(true){
     String line=sc.nextLine();
     if(line.contains("LANTALK"))
     System.out.println(line);
     if(line.contains("END"))
      break;
     logFile.println(line);

   }

   logFile.close();

   try{
     // assigns the file to a filereader object..this will throw an error if  the file does not exist or cannot be found
     BufferedReader infile = new BufferedReader(new FileReader(log));

   try
   {
     // read data from a file..this will throw and error if something goes wrong reading (empty or past end of file)
     while((textLine = infile.readLine()) != null)
   {
     //System.out.printf("%s\n",textLine);
     outLine = textLine.toUpperCase();
     outFile.println(outLine);
     System.out.println(outLine);
   }// end of while 
   } // end of try

  finally  // finally blocks get executed even if an exception is thrown 
   {
     infile.close();
     outFile.close();
   }
   }// end of try

 catch (FileNotFoundException nf) // this goes with the first try because it  will throw a FileNotFound exception
   {
     System.out.println("The file \""+log+"\" was not found"); 
   }
 catch (IOException ioex) // this goes with the second try because it will throw an IOexception
   {
     System.out.println("Error reading the file");
   }

   } /// end of main

 } // end of class

如果这不是您要查找的内容,我很抱歉,但我试图让它确实希望您描述您想要的,我的意思是它确实将输出写入文件,并让输出显示在编译器中,这是编译器控制台的样子:

Please enter full name of the file: test.txt
hi
hi
hi
END

HI
HI
HI

如果这不是您想要的,我很抱歉,但我已尽力而为,希望对您有所帮助。

【讨论】:

  • 非常感谢您的回复!我已经查看并尝试了它,但我仍然遇到同样的问题。我输入的文本似乎创建了一个文件空白文件,命名为我输入的任何内容。并且输入框仍然处于打开状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 1970-01-01
  • 2012-04-06
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多