java的文件操作注意事项

在java中文件操作,创建文件的时候注意:举个例子说明问题

将c:/temp.txt文件 copy 到D:/test/java/file/这个目录下创建一个文件:temp.txt。

这样容易遗漏掉一个问题:

如果不存在路径d:/text/java/file。也就是说d盘肯可能根本就没有text这个文件夹,或者d:/text文件夹内没有java文件夹,或者d:/text/java文件夹没有file文件夹。会出现什么情况?

起初我认为系统会帮助我们创建,其实,不是的。

如果在目的地没有这个路径,会出现FileNotFound异常。

所有在进行文件创建操作一定要分为两步:

1.判断是否路径存在 2.创建文件

String filePath = "d://text/java/file//temp.txt";
String dirPath = filePath.substring(filePath,0,filePath.lastIndexOf("//"));
File dir = new File(dirPath);//1.创建文件所在的目录
if(!dir.exists()){
    dir.mkdirs();//注意mkdir()方法的区别
}
File file = new File(filePath);
if(!file.exists()){
   file.createNewFile();
}
FileInputStream in = new FileInputStream(new File("c://temp.txt"));
FileOutputStream out  = new FileOutputStream(file);
int len = 0;
byte[] buffer = new byte[128];
while((len=in.read(buffer))>0){
    out.write(buffer,0,len);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入流读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len
Java 文件分片上传也需要注意以下几点: 1. 文件切分大小:文件分片大小应该根据网络状况和服务器资源限制进行调整。如果分片过大,可能会导致上传过程中出现超时或者内存溢出等问题;如果分片过小,则会增加上传请求的数量,降低上传效率。 2. 分片顺序:上传分片时需要按照顺序进行上传,以确保服务器可以正确地合并所有分片。可以在上传前对分片按照文件顺序进行排序。 3. 分片重传:如果某个分片上传失败,为了确保上传成功,需要对失败的分片进行重传。可以设置一个最大重试次数,避免无限次重试。 4. 断点续传:如果上传过程中出现网络故障或者用户中断上传,可以考虑使用断点续传功能。断点续传可以记录上传的分片的位置和状态,以便在下次上传时从上次中断的位置继续上传。 5. 安全性:文件上传过程中需要确保数据的安全性,避免上传过程中数据被篡改或者泄露。可以使用 HTTPS 协议进行加密传输,或者对上传的文件进行加密处理。 6. 并发性:为了提高上传效率,可以考虑使用多线程或者异步上传方式。但是需要注意并发上传的数量不能超过服务器的处理能力,否则可能会导致服务器负载过高或者网络拥塞。 总之,文件分片上传需要综合考虑上传效率、网络限制、服务器资源、安全性等多个方面,才能实现高效、稳定、安全的文件上传。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值