//从输入流中读取数据的下一个字节。返回0-255内的int字节值。如果流已经到达末尾,返回-1
read()
//从输入流中读入一定长度的字节,并以整数的形式返回字节数
read(byte[] b)
//关闭此输入流并释放与该流关联的所有系统资源
close()
//返回此输入流在不受阻塞情况下能读取的字节数
available()
3. Reader子类
4. OutputStream类常用方法与子类
//将指定的字节写入此输出流
write(int b)
//将b个字节从指定的byte数组写入此输出流
write(byte[] b)
//彻底完成输出并清空缓存区
flush()
//关闭输出流
close()
File file = new File(strFileFullPath);
if(file.exists()){//判断文件是否存在
file.delete();//删除文件
} else {
file.createNewFile();//创建文件
}
File file = new File("word.txt");//创建文件对象
//文件字节输入流
try {
FileOutputStream out = new FileOutputStream(file);//创建FileOutputStream对象
byte[] b = "我有一只小毛驴,我从来也不骑。".getBytes();//创建byte数组
out.write(b);//将byte数组中的信息写入文件中
out.close();//关闭流
} catch (Exception e) {
e.printStackTrace();//输出异常信息
}
//文件字节输出流
try {
FileInputStream in = new FileInputStream(file);
byte[] bye = new byte[1024];//创建byte数组
int len = in.read(bye);//从文件中读取信息
System.out.println(new String(bye,0,len));
in.close();
} catch (Exception e) {
e.printStackTrace();
}
//构造方法,默认创建一个32字节的缓存区
BufferedInputStream(InputStream in);
//构造方法,创建一个指定字节的缓存区
BufferedInputStream(InputStream in,int size);
//BufferedOutputStream构造方法与BufferedInputStream类似,Buffered流关闭前要调用流的flush()方法
//下载文件接口获取OutputStream用response.getOutputStream()
//excel文件写入输出流用wb.write(BufferedOutputStream)
12.BufferedReader与 BufferedWriter
//BufferedReader常用方法
read()//读取单个字符
readLine()//读取一行字符
//BufferedWriter常用方法
write(String s,int offset,int len)//写入字符串的一部分
flush()//刷新该流的缓存
newLine()//写入一个行分隔符
// BufferedReader代码示例:
/**
1. 获取widnows网卡的mac地址.
2. @return mac地址
*/
public static String getWindowsMACAddress(String ip) {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
/**
* windows下的命令,显示信息中包含有mac地址信息
*/
process = Runtime.getRuntime().exec("nbtstat -A " + ip);
bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream(), "GBK")); // windows系统都是GBK编码,不加GBK读出的中文是乱码
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
/**
* 寻找标示字符串[physical address]
*/
index = line.toLowerCase().indexOf("mac 地址 =");
if (index != -1) {
index = line.indexOf("=");
if (index != -1) {
/**
* 取出mac地址并去除2边空格
*/
mac = line.substring(index + 1).trim();
}
break;
}
}
} catch (IOException e) {
e.getMessage();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
//代码示例
/**
* 功能:压缩多个文件成一个zip文件 *
* @param srcfile:源文件列表
* @param zipfile:压缩后的文件
*/
public static void zipFiles(List<File> srcfile, File zipfile) {
byte[] buf = new byte[1024];
try {
// ZipOutputStream类:完成文件或文件夹的压缩
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
for (int i = 0; i < srcfile.size(); i++) {
FileInputStream in = new FileInputStream(srcfile.get(i));
out.putNextEntry(new ZipEntry(srcfile.get(i).getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
System.out.println("压缩完成.");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 功能:解压缩
* @param zipfile:需要解压缩的文件
* @param descDir:解压后的目标目录
*/
public static void unZipFiles(File zipfile, String descDir) {
try {
ZipFile zf = new ZipFile(zipfile);
for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zf.getInputStream(entry);
OutputStream out = new FileOutputStream(descDir + zipEntryName);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
in.close();
out.close();
System.out.println("解压缩完成.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
/**
* @param response 响应
* @Description: 文件下载
*/
public static void downFile(String filePath, HttpServletResponse response, HttpServletRequest request) throws Exception{
//文件名
File file = new File(filePath);
String fileName = file.getName();
// 设置response的Header 防止文件名乱码;Content-disposition: attachment表示浏览器默认下载文件;
response.addHeader("Content-Disposition","attachment;filename="
+ new String((fileName).getBytes("utf-8"), "ISO-8859-1"));
//告知浏览器这是一个字节流,浏览器处理字节流的默认方式就是下载。
response.setContentType("application/octet-stream");
InputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
IOUtils.copy(in, out);
out.flush();
in.close();
}