从输入日志写入文件

时间:2017-06-28 16:26:14

标签: java file input writing

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

git bisect

1 个答案:

答案 0 :(得分:0)

你需要的是在while循环结束while(sc.hasNext())因为Scanner sc总是会有下一个因为你真的在问自己是否从用户那里得到了这条线然后等待下一行sc.nextLine();然后你把它放到一个字符串中,所以下次你问自己我有这条线的答案是肯定的,反正它有点复杂来解决这个问题你需要改变while循环来得到一个特殊的词会制动它,所以你必须改变它:

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

To,例如:

   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

对不起,如果这不是你想要的,但我尽力了,希望有所帮助。