日期:2014-05-20 浏览次数:20917 次
public static void main(String[] args) throws IOException{ File f = new File("F:/", "case.txt"); FileInputStream fis = new FileInputStream(f); byte[] b = new byte[(int)f.length()]; System.out.println(f.length()); fis.read(b); File fi = f.createTempFile("CopyNameClass", ".java", new File("f:/")); FileOutputStream fou = new FileOutputStream(fi); fou.write(b); fou.close(); fis.close(); }
//java.io.File private static File generateFile(String prefix, String suffix, File dir) throws IOException { if (counter == -1) { counter = new Random().nextInt() & 0xffff; } counter++; return new File(dir, prefix + Integer.toString(counter) + suffix); }
------解决方案--------------------
这是因为你调用的File.createTempFile()这个API,这个本来就是生成临时文件的,生成之后最后一般要删除,所以生成文件名称JVM有个标记
如果你不想有这个,就不应该调用上面的API,改成下面的
import java.io.*; public class FileWriterDemo { public static void main(String[] args) throws IOException{ File f = new File("F:/", "case.txt"); FileInputStream fis = new FileInputStream(f); byte[] b = new byte[(int)f.length()]; System.out.println(f.length()); fis.read(b); // File fi = f.createTempFile("CopyNameClass", ".java", new File("f:/")); File fi = new File("F:/CopyNameClass.txt"); FileOutputStream fou = new FileOutputStream(fi); fou.write(b); fou.close(); fis.close(); } }