File中的一些方法
File file = new File("C:/Users/Administrator/Desktop/GG");
file.createNewFile(); 创建文件
file.mkdirs(); 创建文件夹
file.delete(); 删除
file.isDirectory(); 判断是否是目录
file.isFile(); 判断是否是文件
file.exists(); 判断是否存在
file.canRead(); 判断是否可读
file.canwrite(); 判断是否可写
file.isHidden() 判断是否隐藏
file.getAbsolutepath(); 获取绝对路径
file.getpath(); 获取路径
file.getName();
file.length(); 获取长度 (Long类型)
file.lastModified(); 获取最后一次的修改时间 (Long类型)
file.list(); 获取文件夹下所有东西(String类型)
file.listFile(); 获取文件夹下所有东西(File类型—含路径)
File、FileInputStream、ByteArrayOutputStream
File file = new File("C:/Users/Administrator/Desktop/GG");
File filedoc = new File(file+"/gg.doc");
if (!file.exists()) {
log.info("文件夹不存在,创建文件夹");
file.mkdirs();
}
if (!filedoc.exists()) {
log.info("文件不存在,创建文件");
filedoc.createNewFile();
}
FileOutputStream out = new FileOutputStream(filedoc,true);
FileInputStream in = new FileInputStream(new File("C:/Users/Administrator/Desktop/1.doc"));
ByteArrayOutputStream bytewrite = new ByteArrayOutputStream();
int i = 0;
while((i=in.read()) != -1){
bytewrite.write(i);
}
byte[] aby = bytewrite.toByteArray();
String str = new String(aby,0,aby.length);
log.info("读取到1.doc文件数据为:"+str);
out.write(str.getBytes());
bytewrite.close();
out.close();
in.close();
ServletOutputStream流,写入字节数组
public void showfileUpload(HttpServletResponse response, String id) {
log.info("读取图片信息 ");
try {
String filenameUrl = FILEUPLOAD_PATH+id+".jpg";
FileInputStream fis = new FileInputStream(new File(filenameUrl));
ByteArrayOutputStream bytewrite = new ByteArrayOutputStream();
int i = 0;
while((i=fis.read()) != -1){
bytewrite.write(i);
}
byte[] bs = bytewrite.toByteArray();
response.setContentType("image/jpeg; charset=utf-8");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(bs);
} catch (Exception e) {
log.info("读取信息异常"+e);
}
}
InputStream(字节流) —> InputStreamReader(字符流) —> BufferedReader(字符缓冲区流)
String str = "";
InputStream in = request.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
BufferedReader bd = new BufferedReader(reader);
while ((inputLine = bd.readLine()) != null) {
str += inputLine;
}