2015-10-14 97 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); 

这似乎解决了我的问题。 感谢您的帮助,我无法在没有您的情况下完成任务!

相关问题