简体   繁体   English

从输入日志写入文件

[英]Writing to File From Input Log

So I'm having a few troubles here.所以我在这里遇到了一些麻烦。 I need to be able to write my output to a file, and have it contain only the keywords specified in the code.我需要能够将我的输出写入一个文件,并让它只包含代码中指定的关键字。 This code is writing nothing to the file, and it only opens another box for user input.此代码没有向文件写入任何内容,它只会打开另一个框供用户输入。 How do I get it to close the input box after the user inputs the file name, get it to write the output to the file, and get the output to display in the compiler?我如何让它在用户输入文件名后关闭输入框,让它将输出写入文件,并让输出显示在编译器中? Thanks!谢谢!

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

What you need is to end the while(sc.hasNext()) while loop because the Scanner sc will always have a next because you are literally saying asking yourself if you got the line from the user then wait for next line with sc.nextLine();您需要结束 while(sc.hasNext()) while 循环,因为 Scanner sc 将始终有一个下一个,因为您实际上是在问自己是否从用户那里得到该行,然后使用 sc.nextLine 等待下一行(); then you are putting it into a string so next time you ask yourself do i have the line the answer is yes,anyways it's a little complicated to get over this issue you need to change the while loop to have a special word that will brake it,so you have to change it from:然后你把它放到一个字符串中,所以下次你问自己我有没有这条线时,答案是肯定的,无论如何克服这个问题有点复杂,你需要更改 while 循环以有一个特殊的词来阻止它,所以你必须改变它:

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

To,for example:例如:

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

Also you need to check if the file entered by the user exists and actually add the text from the console to the file,so it would look something like this:您还需要检查用户输入的文件是否存在,并将控制台中的文本实际添加到文件中,所以它看起来像这样:

 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();

Now all we have to do is print the output to the console when writing it to the logFile,so the while((textLine = infile.readLine()) != null),will now look a little something like this:现在我们要做的就是在将输出写入 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

So in the end the hole thing should look a little something like this:所以最后这个洞应该看起来像这样:

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

If this is not what you are looking for i'm sorry,but i tried to make it do want you described you wanted,i mean it does write the output to the file, and get the output to display in the compiler,here's what the compiler console looks like:如果这不是您要查找的内容,我很抱歉,但我试图让它确实希望您描述您想要的内容,我的意思是它确实将输出写入文件,并让输出显示在编译器中,这就是编译器控制台如下所示:

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

HI
HI
HI

I'm sorry if this is not what you wanted but i tried my best,hope it helps.如果这不是您想要的,我很抱歉,但我已尽力而为,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM