简体   繁体   English

当我使用PrintWriter写入文件时,如何摆脱0D输出?

[英]How can I get rid of the 0D output when I use PrintWriter to write to file?

code is working fine, I'm just curious as to how I can prevent the char 0D from printing at the end of my text file if at all possible. 代码工作正常,我只是好奇如何尽可能防止char 0D在我的文本文件末尾打印。 Thank you. 谢谢。 here's the code if it helps. 这是代码,如果它有帮助。 sorry if it's mediocre, still learning java. 对不起,如果它是平庸的,还在学习java。

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

   public class Driver{
      String inputFile, input;
      Scanner kb = new Scanner(System.in);
      PrintWriter output = new PrintWriter(System.out);
      File file; 
      BST tree = new BST();
      Trie trie = new Trie();


      public static void main(String[] args){
         Driver driver = new Driver();
         boolean done = false;  

         driver.initialDataFromFile();
         while (!done){      
            done = driver.menu(true);   
         }
      }

      public void initialDataFromFile() {
         System.out.println("Welcome to Project0");
         System.out.println("What is the name of the input file?");
         System.out.print("> ");
         inputFile = kb.nextLine(); 
         File file = new File(inputFile);

         try{
            Scanner scanner = new Scanner(file);

            while(scanner.hasNextLine()){
               String in = scanner.nextLine();
               tree.insert(in);
               trie.insert(in);
            }
            System.out.println("Finished reading the file");

         }
            catch(FileNotFoundException e){
               System.out.println("File does not exist");
               System.exit(1);
            }
      }

      public boolean menu(boolean done){      
         System.out.print("Please enter your command\r>");
         input = kb.next();
         File file = new File(inputFile);
         PrintWriter output = null;// = new PrintWriter(inputFile);

         if(input.toLowerCase().equals("quit")){
            System.out.println("Quitting");
            System.out.println("Writing updated info to " + inputFile);     
            try{
               output =  new PrintWriter(file);
             //   String str = "";
//                str += tree.toString();
               output.println(tree.toString());  

               output.close();

            }
               catch(IOException e){
                  System.out.print("error");}
            System.out.println("Goodbye");    
            return true;       
         }

         else if(input.toLowerCase().equals("insert")){
            input = kb.next();
            tree.insert(input);
            trie.insert(input);
            System.out.println();
            return false;
         }
         else if(input.toLowerCase().equals("delete")){
            input = kb.next();
            tree.delete(input);
            trie.delete(input);
            System.out.println();  
            return false;
         }
         else if(input.toLowerCase().equals("find")){
            input = kb.next();
            tree.find(input);
            trie.find(input);
            System.out.println();
            return false;
         }
         else if(input.toLowerCase().equals("print")){
            tree.print();
            trie.print();
            System.out.println();
            return false;
         }
         else{
            System.out.println("Not A Valid Choice");
            System.out.println();
            return false;
         }
      }
   }
printWriter.println("...")

will use OS dependant line.separator property to determine line separator sequence: 将使用OS相关的line.separator属性来确定行分隔符序列:

These methods use the platform's own notion of line separator rather than the newline character. 这些方法使用平台自己的行分隔符概念而不是换行符。

It happens to be \\r (?) on your computer ( OD ) (from JavaDoc of PrintWriter.println() ): 它恰好是您计算机上的\\r (?)( OD )(来自PrintWriter.println() JavaDoc):

The line separator string is defined by the system property line.separator , and is not necessarily a single newline character ( '\\n' ). 行分隔符字符串由系统属性line.separator定义,不一定是单个换行符( '\\n' )。

Just use plain print() and attach whichever character you want: 只需使用普通print()并附加您想要的任何字符:

printWriter.print("...\n")

...or override the line.separator property. ...或覆盖line.separator属性。

You can use the line.separator system property, eg, to provide OS-independent output. 您可以使用line.separator系统属性,例如,提供与OS无关的输出。

http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

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

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