これはやや曖昧です。両方のファイルのコピーで次のバッチスニペット結果:Javaでファイルをコピーすると、2つの空白が連続したファイル名がスキップされます
xcopy "C:\Source\Spaces1 [ ].txt" "C:\Target\" /Y
xcopy "C:\Source\Spaces2 [ ].txt" "C:\Target\" /Y
そして、次のJavaはまた、両方のファイルをコピーし、その結果使用したストリームをスニペット:このスニペットでは、
public static void main(final String args[]) throws IOException
{
final File source1 = new File("C:\\Source", "Spaces1 [ ].txt");
final File target1 = new File("C:\\Target", "Spaces1 [ ].txt");
fileCopy(source1, target1);
final File source2 = new File("C:\\Source", "Spaces2 [ ].txt");
final File target2 = new File("C:\\Target", "Spaces2 [ ].txt");
fileCopy(source2, target2);
}
public static void fileCopy(final File source, final File target) throws IOException
{
try (InputStream in = new BufferedInputStream(new FileInputStream(source));
OutputStream out = new BufferedOutputStream(new FileOutputStream(target));)
{
final byte[] buf = new byte[4096];
int len;
while (0 < (len = in.read(buf)))
{
out.write(buf, 0, len);
}
out.flush();
}
}
しかし、ファイルの一つではありませんコピーされたもの(二重スペースのものはスキップされます):
public static void main(final String args[]) throws Exception
{
final Runtime rt = Runtime.getRuntime();
rt.exec("xcopy \"C:\\Source\\Spaces1 [ ].txt\" \"C:\\Target\\\" /Y").waitFor();
// This file name has two spaces in a row, and is NOT actually copied
rt.exec("xcopy \"C:\\Source\\Spaces2 [ ].txt\" \"C:\\Target\\\" /Y").waitFor();
}
どうしますか?これは、誰が何を入力することができるのか、誰が好きなものであれ、どのソースからファイルをコピーするのに使用されます。ファイル名はサニタイズされていますが、2つのスペースを連続して消毒するのは誰ですか?私はここで何が欠けていますか?
現在Java 8を使用していますが、Java 6と7では同じ結果が得られます。
スラッシュでエスケープしようとしましたが(動作しません)、スペースを '%20'(動作しません)に置き換えようとしました。そして、はい、私はすでにRobocopyを使用できることを知っています。 –
最初にダブルスペースファイルをコピーしてから、シングルスペースファイルをコピーしても同じ結果が得られましたか? –
はい、私はそれを試みました。良い提案ですが、同じ結果です。 –