使用键盘输入将文本文件复制到另一个位置的程序

时间:2014-05-03 14:42:24

标签: java io undefined-symbol

我正在编写一个程序,将文本文件从键盘输入复制到另一个文件位置,但是遇到了麻烦。从键盘输入时文件名位于正确的位置,但内容不存在。我有一行正确复制到下一个文件。目前,我收到第41行无法找到符号错误。

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

    public class lity
    {
     public static void main(String[] args)
     {
      Scanner keyboard = new Scanner(System.in);
      System.out.println("Directory of file to be copied");
      String dir = keyboard.nextLine();
      System.out.println("Name of file to be copied");
      String file = keyboard.nextLine();
      File copied = new File(dir, file);

      BufferedReader in =new BufferedReader(new FileReader(copied));
      String str;

      while((str = in.readLine())!=null)
      {
       char[] n;
       n = str.toCharArray();
      }
      in.close();



      System.out.println("Path of pasted file");
      String pdir = keyboard.nextLine();
      File newdir = new File(pdir);  

      if(!newdir.exists())
      {
       newdir.mkdir();
      }

      System.out.println("pasted file name");
      String pfile = keyboard.nextLine();
      File myfile = new File(newdir, pfile);

      FileWriter stream = new FileWriter(myfile);

      stream.write(n);
      stream.close();
      }

     }

新代码

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

    public class CopyFile
    {
     public static void main(String[] args)
     {
      String s = " ";
      StringBuilder sb = new StringBuilder(s);


      Scanner keyboard = new Scanner(System.in);
      System.out.println("enter directory path of the file to be pasted:");
      String dirName = keyboard.next();
      System.out.println("enter file name of the file to be copied");
      String fileName = keyboard.next();
      try
      {
       File input = new File(dirName, fileName);



       Scanner ind = new Scanner(input);  
       BufferedReader in = new BufferedReader(new FileReader(input));
       String str;

       try
       {
        while(ind.hasNextLine())
        {
         str = in.readLine(); 
         sb.append(str+"\n");
        }
        in.close();
        ind.close();
       }

       catch(IOException e)
       {
        System.out.println("IOException");
       }

       System.out.println("path of file to be pasted");
       String dir = keyboard.next();
       File myDir = new File(dir);

       if(!myDir.exists())
       {
        myDir.mkdir();
       }   


       System.out.println("enter name of the file to be pasted");
       String mfile;
       mfile = keyboard.next();
       try
       {
        File myFile = new File(myDir, mfile);

        PrintWriter pw = new PrintWriter(new FileWriter(myFile));

        String nstr;
        nstr = sb.toString();
    char[] n = nstr.toCharArray();
        for(int d = 0;d < n.length; d++)
    {
     pw.write(n[d]);
    }
        pw.close();
       }
      catch(IOException e)
      {
       System.out.println(e);
      }
     }
       catch(FileNotFoundException e)
       {
        System.out.println(e);
       }
      }
     }

1 个答案:

答案 0 :(得分:0)

您在循环内部声明n变量,然后尝试在其他地方使用它。要获得编译代码,您需要在main方法或类中声明它。

无论如何,即使编译了代码,它仍然会失败,因为您在从文件读入的每个循环中重新创建char数组。考虑使用StringBuilder获取所有字符串,或者在一个循环中同时读取和写入。

类似(伪代码):

create StringBuilder
Open file to read in a Scanner or BufferedReader
Loop through text of file
  append each String from file into the StringBuilder
  You probably want to append "\n" with each line added
end loop
close the Scanner or BufferedReader

Open file to write and create a PrintWriter with it
write out StringBuilder's String using toString() into the file using PrintWriter
close the PrintWriter.