2015-10-14 96 views
0

我正在研究項目以讀取,寫入,格式化並更改桌面上預定文件的名稱。除了namechange方法外,一切工作都正常。當我輸入更改名稱併爲其提供新文件時,它會使用舊文本文件的位置填充新文本文件。使用文本文件複製到新的文本文件

更新!我發現了這個問題,但我仍然沒有答案。似乎無論將哪些信息放入 Scanner inputfile = new Scanner(「here」); 這裏將被打印出來不是文件的內容。因此,當下一步到達時,它讀取應該掃描的文件,而不是掃描它的這裏的部分,並將其打印到文件變量,然後打印到輸出和隨後的文件。

package file.io; 

import java.io.*; 
import java.util.Scanner; 
import java.lang.StringBuilder; 

public class FileIo { 


public static void main(String[] args) { 

    Scanner keyboard = new Scanner(System.in); 

    String answer; 
    int x = 0; 
    String loop; 

    while (x < 1) { 
     int y = 0; 
     System.out.println("what would you like to do read, write, format, " 
       + "or change the name?"); 
     answer = keyboard.nextLine(); 
     if (answer.equalsIgnoreCase("Read")) { 
      read(); 
     } else if (answer.equalsIgnoreCase("write")) { 
      write(); 
     } else if (answer.equalsIgnoreCase("format")) { 
      format(); 
     } else if (answer.equalsIgnoreCase("change the name")) { 
      namechange(); 
     } else { 
      System.out.println("you entered an invalid function."); 
     } 
     while (y < 1) { 
      System.out.println("would you like to do another opperation?"); 
      loop = keyboard.nextLine(); 

      if (loop.equalsIgnoreCase("no")) { 
       x++; 
       y++; 
       System.out.println("goodbye!"); 
      } else if (loop.equalsIgnoreCase("yes")) { 
       System.out.println("\n"); 
       y++; 
      } else { 
       System.out.println("the possible answers are yes or no, try again."); 
      } 
     } 
    } 

} 

public static void read() { 

    try { 
     String file = "C:\\Users\\danor\\Desktop\\example.txt"; 
     File fileHandle = new File(file); 
     try (Scanner inputfile = new Scanner(fileHandle)) { 
      String line; 
      if (inputfile.hasNextLine()) { 
       while (inputfile.hasNextLine()) { 
        line = inputfile.nextLine(); 
        System.out.println(line); 
       } 
       System.out.println("\n"); 
      } else { 
       System.out.println("the file contains nothing."); 
      } 
     } 
    } catch (Exception e) { 
     System.out.println("something went wrong"); 
    } 

} 

public static void write() { 
    Scanner keyboard = new Scanner(System.in); 
    String print; 
    try { 
     String file = "C:\\Users\\danor\\Desktop\\example.txt"; 
     FileWriter fwriter = new FileWriter(file, true); 
     PrintWriter out = new PrintWriter(fwriter); 
     System.out.println("what would you like to print?"); 
     print = keyboard.nextLine(); 
     out.println(print); 
     out.close(); 
    } catch (Exception e) { 
     System.out.println("something went wrong"); 

    } 
} 

public static void format() { 
    try { 
     String file = "C:\\Users\\danor\\Desktop\\example.txt"; 
     PrintWriter writer = new PrintWriter(file); 
     writer.print(""); 
     writer.close(); 
     System.out.println("file formatted."); 
    } catch (Exception e) { 
     System.out.println("something went wrong"); 
    } 
} 

public static void namechange() { 
    try { 
     String fileorig = "C:\\Users\\danor\\Desktop\\example.txt"; 
     String asf; 
     String line; 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("what do you want the new file to be named."); 
     StringBuilder file = new StringBuilder(); 
     file.append("C:\\Users\\danor\\Desktop\\"); 
     file.append(keyboard.nextLine()); 
     file.append(".txt"); 
     asf = file.toString(); 
     try { 
      FileWriter fwriter = new FileWriter(asf, true); 
      PrintWriter out = new PrintWriter(fwriter); 
      Scanner inputfile = new Scanner(fileorig); 
      if (inputfile.hasNextLine()) { 
       while (inputfile.hasNextLine()) { 
        line = (inputfile.nextLine()); 
        out.println(line); 
        out.close(); 
       } 
       System.out.println("\n"); 
      } else { 
       System.out.println("the file contains nothing."); 
      } 
     } catch (FileNotFoundException | UnsupportedEncodingException e) { 
      System.out.println("something went wrong with the writing"); 
     } 

    } catch (Exception e) { 
     System.out.println("there was something wrong witht the reading"); 

    } 
} 

} 
+0

你似乎是在完成寫作之前,過早地關閉'out'。查看[The try-with-resources Statement](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)以獲得更好的解決方案來處理資源管理 – MadProgrammer

+0

'renameTo()'類File的方法,這是你想要的嗎? –

回答

1

在完成寫入新文件之前,您正在關閉您的PrintWriter對象。

public static void namechange() { 
    try { 
     String fileorig = "C:\\Users\\danor\\Desktop\\example.txt"; 
     String asf; 
     String line; 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("what do you want the new file to be named."); 
     StringBuilder file = new StringBuilder(); 
     file.append("C:\\Users\\danor\\Desktop\\"); 
     file.append(keyboard.nextLine()); 
     file.append(".txt"); 
     asf = file.toString(); 
     try { 
      FileWriter fwriter = new FileWriter(asf, true); 
      PrintWriter out = new PrintWriter(fwriter); 
      Scanner inputfile = new Scanner(fileorig); 
      if (inputfile.hasNextLine()) { 
       while (inputfile.hasNextLine()) { 
        line = (inputfile.nextLine()); 
        out.println(line); 
       } 
       System.out.println("line"); 
      } else { 
       System.out.println("Reached the end of the file"); 
      } 

      //close the PrintWriter when you finish writing to the file 
      out.close(); 

     } catch (FileNotFoundException | UnsupportedEncodingException e) { 
      System.out.println("something went wrong with the writing : " + e.getMessage()); 
     } 

    } catch (Exception e) { 
     System.out.println("there was something wrong witht the reading : " + e.getMessage()); 

    } 
} 
+0

看起來你的代碼在舊文件的目的地上有變量行,而不是文件內部的變量行。我試過這個,得到** C:\ Users \ danor \ Desktop \ example.txt **,而不是舊文件中包含的「Hello Dan」。 –

0

,如果你想改變你的文件的名稱,然後File類IO包的提供方法renameTo

public static void namechange() { 

     String fileorig = "C:\\Users\\danor\\Desktop\\example.txt"; 
     File file = new File(fileorig); 
     Scanner scanner = new Scanner(System.in); 
     StringBuilder sb = new StringBuilder("C:\\Users\\danor\\Desktop\\"); 
     sb.append(scanner.nextLine()); 
     sb.append(".txt"); 
     file.renameTo(new File(sb.toString())); 
     scanner.close(); 
}  
+0

雖然這工作,即時嘗試使用原始文件的內容和一個新的名稱,而不是簡單地重命名文件的新文件。我有代碼製作新文件,但是新文件的內容是: ** C:\ Users \ danor \ Desktop \ example.txt ** 看起來有些東西導致掃描儀拾起位置舊的文件,而不是它裏面的東西。 –

0

我剛找到答案。在查看一些其他代碼並閱讀掃描儀後,看起來我忘了設置文件。咄。我所需要的只是一個簡單的。

File origional = new File(fileorig); 

這似乎解決了我的問題。 感謝您的幫助,我無法在沒有您的情況下完成任務!

相關問題