简体   繁体   中英

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. Thank you. here's the code if it helps. sorry if it's mediocre, still learning 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:

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

The line separator string is defined by the system property line.separator , and is not necessarily a single newline character ( '\\n' ).

Just use plain print() and attach whichever character you want:

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

...or override the line.separator property.

You can use the line.separator system property, eg, to provide OS-independent output.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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