在java中创建一个目录

发布于 2024-11-06 19:38:44 字数 1980 浏览 4 评论 0原文

我正在尝试创建一个目录并将文件复制到其中。我已经实现的代码及其输出如下。这个问题似乎是不言自明的,但我会为那些无法分辨的人明确说明。

问题


无论我做什么,我似乎都无法创建复制文件所需的目标文件。

代码


get是要复制的文件,dest是将复制到的目录。为了清楚起见,添加了行号和“ERR>”。我已经注释掉了我尝试过的其他文件创建方法,但它们都失败了。

115:  private void copyTo(File get, File dest)
116:  {
117:    try
118:    {
119:      dest = new File((dest.getPath().endsWith(File.separator) ? dest.getPath() : dest.getPath() + File.separator) + get.getName());
120:      
121:      java.io.FileInputStream fis = new java.io.FileInputStream(get);
122:      if (dest.exists())
123:        while(!dest.delete());
124:      dest.mkdir();
125://      dest.createNewFile();
126://      java.io.FileWriter w = new java.io.FileWriter(dest);
127://      w.write("");
128:      System.out.println("Writing \"" + get + "\" to \"" + dest + "\"");
129:ERR>  java.io.FileOutputStream fos = new java.io.FileOutputStream(dest);
130:      int b;
131:      do
132:      {
133:        b = fis.read();
134:        fos.write(b);
135:      }while (b != -1);
136:    }
137:    catch (FileNotFoundException ex)
138:    {
139://      System.err.println("404: \"" + get + "\"");
140:      ex.printStackTrace();
141:    }
142:    catch (java.io.IOException ex)
143:    {
144://      System.err.println("IO exception on \"" + get + "\"");
145:      ex.printStackTrace();
146:    }
147:  }

输出


Writing "J:\warehouse.txt" to "J:\backup\warehouse.txt"
java.io.FileNotFoundException: J:\backup\warehouse.txt (The system cannot find the path specified)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
        at copy.TUI.copyTo(TUI.java:129)
        at copy.TUI.copy(TUI.java:110)
        at copy.TUI.run(TUI.java:102)
        at copy.Main.main(Main.java:37)

I'm trying to create a directory and copy files to it. The code I've implemented and its output is below. The problem seems self explanatory, but I'll make it explicit for those who can't tell.

Problem


No matter what I do, I can't seem to create the destination file required to copy the file.

Code


get is the file to be copied, and dest is the directory to which it will be copied. Line numbers and "ERR>" were added for clarity. I've commented out other methods of file creation which I've tried, but they all failed.

115:  private void copyTo(File get, File dest)
116:  {
117:    try
118:    {
119:      dest = new File((dest.getPath().endsWith(File.separator) ? dest.getPath() : dest.getPath() + File.separator) + get.getName());
120:      
121:      java.io.FileInputStream fis = new java.io.FileInputStream(get);
122:      if (dest.exists())
123:        while(!dest.delete());
124:      dest.mkdir();
125://      dest.createNewFile();
126://      java.io.FileWriter w = new java.io.FileWriter(dest);
127://      w.write("");
128:      System.out.println("Writing \"" + get + "\" to \"" + dest + "\"");
129:ERR>  java.io.FileOutputStream fos = new java.io.FileOutputStream(dest);
130:      int b;
131:      do
132:      {
133:        b = fis.read();
134:        fos.write(b);
135:      }while (b != -1);
136:    }
137:    catch (FileNotFoundException ex)
138:    {
139://      System.err.println("404: \"" + get + "\"");
140:      ex.printStackTrace();
141:    }
142:    catch (java.io.IOException ex)
143:    {
144://      System.err.println("IO exception on \"" + get + "\"");
145:      ex.printStackTrace();
146:    }
147:  }

Output


Writing "J:\warehouse.txt" to "J:\backup\warehouse.txt"
java.io.FileNotFoundException: J:\backup\warehouse.txt (The system cannot find the path specified)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
        at copy.TUI.copyTo(TUI.java:129)
        at copy.TUI.copy(TUI.java:110)
        at copy.TUI.run(TUI.java:102)
        at copy.Main.main(Main.java:37)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

時窥 2024-11-13 19:38:45

使用以下 -

dest = dest.isDirectory() ?  new File(dest, get.getName()):  new File(dest.getParentDirectory(), get.getName());
dest.getParentDirectory().mkdirs();

Use following -

dest = dest.isDirectory() ?  new File(dest, get.getName()):  new File(dest.getParentDirectory(), get.getName());
dest.getParentDirectory().mkdirs();
挽心 2024-11-13 19:38:44

使用dest.getParentFile().mkdir()。这将为您的目标文件创建父目录。如果可能缺少多个父路径元素,您可以使用 mkdirs() 方法递归创建所有缺少的目录。

Use dest.getParentFile().mkdir(). This will create the parent dir for your dest file. In case multiple parent path elements might be missing you can use the mkdirs() method to create all missing directories recursively.

尾戒 2024-11-13 19:38:44

能够自己修复它。我真是太笨了,一开始就没有看到它......

  private void copyTo(File get, File dest)
  {
    try
    {
      File newDest = new File((dest.getPath().endsWith(File.separator) ? dest.getPath() : dest.getPath() + File.separator) + get.getName());

      java.io.FileInputStream fis = new java.io.FileInputStream(get);
      if (dest.exists())
       dest.delete();
      dest.mkdirs();
      dest.setWritable(true);
      dest.setReadable(true);
//      dest.createNewFile();
//      java.io.FileWriter w = new java.io.FileWriter(dest);
//      w.write("");
      System.out.println("Writing \"" + get + "\" to \"" + dest + "\"");
      java.io.FileOutputStream fos = new java.io.FileOutputStream(newDest);
      int b;
      do
      {
        b = fis.read();
        fos.write(b);
      }while (b != -1);
    }
    catch (FileNotFoundException ex)
    {
//      System.err.println("404: \"" + get + "\"");
      ex.printStackTrace();
    }
    catch (java.io.IOException ex)
    {
//      System.err.println("IO exception on \"" + get + "\"");
      ex.printStackTrace();
    }
  }

Was able to fix it myself. Pretty derpy of me to not see it in the first place...

  private void copyTo(File get, File dest)
  {
    try
    {
      File newDest = new File((dest.getPath().endsWith(File.separator) ? dest.getPath() : dest.getPath() + File.separator) + get.getName());

      java.io.FileInputStream fis = new java.io.FileInputStream(get);
      if (dest.exists())
       dest.delete();
      dest.mkdirs();
      dest.setWritable(true);
      dest.setReadable(true);
//      dest.createNewFile();
//      java.io.FileWriter w = new java.io.FileWriter(dest);
//      w.write("");
      System.out.println("Writing \"" + get + "\" to \"" + dest + "\"");
      java.io.FileOutputStream fos = new java.io.FileOutputStream(newDest);
      int b;
      do
      {
        b = fis.read();
        fos.write(b);
      }while (b != -1);
    }
    catch (FileNotFoundException ex)
    {
//      System.err.println("404: \"" + get + "\"");
      ex.printStackTrace();
    }
    catch (java.io.IOException ex)
    {
//      System.err.println("IO exception on \"" + get + "\"");
      ex.printStackTrace();
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文