我正在尝试创建一个目录并将文件复制到其中。我实现的代码及其输出如下。这个问题似乎不言自明,但我会为那些无法分辨的人明确表示。
问题
无论我做什么,我似乎都无法创建复制文件所需的目标文件。
代码
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)