java 文件(临时文件)操作

	public static void doFile(String fileName) {
		try {
			File file = new File(fileName);
			if (file.exists()) {
				file.createNewFile();
			}
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

public static byte[] File2ByteArray(String pstrFilename) {
		File pTextfd = null;
		long ulFileLength;
		pTextfd = new File(pstrFilename);
		if (!pTextfd.exists()) {
			return null;
		}
		ulFileLength = pTextfd.length();
		FileInputStream fis = null;
		byte[] content = null;
		try {
			fis = new FileInputStream(pTextfd);
			content = new byte[(int) ulFileLength];
			int nRed = fis.read(content);
			int left = 0;
			while ((left = (int) ulFileLength - nRed) > 0) {
				int red = fis.read(content, nRed, left);
				nRed += red;
			}
			fis.close();
			fis = null;

		} catch (Exception e) {
			if (fis != null)
				try {
					fis.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			e.printStackTrace();
		}

		return content;
	}

 

java 创建临时目录与删除 

 获得当前系统的临时目录路径:System.getProperty("java.io.tmpdir")
UUID随机数:java.util.UUID.randomUUID()
创建临时文件:f.createTempFile(prefix, suffix, directory)
删除临时文件:new File(mp3file).deleteOnExit();

 File f = new File("c:\\");
		        try {
		            File.createTempFile("letter",".txt",f);
		            f.deleteOnExit();
		        } catch (IOException e1) {
		            e1.printStackTrace();
		        }
产生的临时文件为c:\\letter9063772511034964436.txt

 

 

你可能感兴趣的:(java,C++,c,C#,F#)