InputStream
InputStream是Java标准库最基本的输入流,在java.io包内。它是一个抽象类。
FileInputStream:从文件中读取数据,是最终数据源。
int read()方法:读取输入流的下一个字节,并返回字节表示的int值(0~255).如果已读到末尾,返回-1表示不能继续读取了。
不同方式读取文件的输入流
package study1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class Demo03 {
public static void main(String[] args) {
// 创建一个基于读取文件方式的输入流
try (FileInputStream in = new FileInputStream("F:\\Test\\Use\\yuan.jpg");){
//方式1:每次读取1个字节数据,读取至末尾时,返回-1
// int data1 = in.read();
// int data2 = in.read();
// int data3 = in.read();
// System.out.println(data1);
// System.out.println(data2);
// System.out.println(data3);
// //通过循环,读取所有字节数据
// int data = -1;
// while((data = in.read()) != -1) {
// System.out.println(data);
// }
//方式2:
// byte[] buff = new byte[256];
// int len = in.read(buff);
// System.out.println("本次读取到的内容:"+Arrays.toString(buff));
// System.out.println("本次读取到的长度:"+len);
byte[] buff = new byte[128];
int len = 0;
while((len = in.read(buff)) > 0) {
System.out.printf("本次读取到%d个字节:%s\n",len,Arrays.toString(buff));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
OutputStream
OutputStream是Java标准库提供的用于写入操作的基础输出流。(抽象类)
void write(int b):写入一个字节到输出流。(虽然传入的是int参数,但只会写入一个字节,即只写入int最低8位表示字节的部分)
package study1;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Demo04 {
public static void main(String[] args) {
try {
// 读取到一张图片的字节数据
byte[] imgData = Files.readAllBytes(Paths.get("F:\\Test\\IOUse\\yuan.jpg"));
// 创建文件输出流
try(FileOutputStream out = new FileOutputStream("F:\\Test\\IOUse\\yi.jpg")){
out.write(imgData);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
边读边写
FileInputStream和FileOutputStream
package study1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo05 {
public static void main(String[] args) {
try (FileInputStream in = new FileInputStream("F:\\Test\\IOUse\\yuan.jpg");
FileOutputStream out = new FileOutputStream("F:\\Test\\IOUse\\ming.jpg");){
byte[] buff = new byte[128];
int len = 0;
while((len = in.read(buff)) > 0) {
out.write(buff,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
read不同用法区别
FileInputStream fis = new FileInputStream();
fis.read();//文件输入流:每次read()方法,都会产生磁盘的读取
BufferedInputStream in =new BufferedInputStream();
in.read();//每次read()方法,先在缓冲区读取,如果缓冲区读取完毕,则统一访问磁盘,进行内容填充
package study1;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class Demo06 {
public static void main(String[] args) {
// FileInputStream fis = new FileInputStream();
// fis.read();//文件输入流:每次read()方法,都会产生磁盘的读取
// BufferedInputStream in =new BufferedInputStream();
// in.read();//每次read()方法,先在缓冲区读取,如果缓冲区读取完毕,则统一访问磁盘,进行内容填充
try (BufferedInputStream in = new BufferedInputStream(
new FileInputStream("F:\\Test\\IOUse\\yuan.jpg"))){
byte[] buff = new byte[128];
int len = 0;
//
//每次read()方法的调用,都会从BufferedInputStream缓冲区进行读取
//每次读取“最多”128个字节
while((len = in .read(buff)) > 0) {
System.out.println(Arrays.toString(buff));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
BufferedInputStream和BufferedOutputStream
BufferedInputStream:缓冲输入流,为另一个输入流添加一些和功能(本质是通过内部缓冲区数组实现的)。使缓冲区提高文件的读取
BufferedOutputStream:可以使用缓冲区对文件进行写入操作
package study1;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo07 {
public static void main(String[] args) {
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream("F:\\Test\\IOUse\\yuan.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("F:\\Test\\IOUse\\haha.jpg"));
byte[] buff = new byte[2048];
int len = 0;
while((len = bis.read(buff)) > 0) {
bos.write(buff, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
读取网络照片到本地磁盘
package study1;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class Demo08 {
public static void main(String[] args) {
String imageUrl = "https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F0315%2Fbc0c9ab1j00sae5710193d001g800lom.jpg&thumbnail=660x2147483647&quality=80&type=jpg";
try {
URL url = new URL(imageUrl);
try(InputStream in = url.openConnection().getInputStream();
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\Test\\IOUse\\king.jpg"))){
byte[] buff = new byte[1024];
int len = 0;
while((len = bis.read(buff)) > 0) {
bos.write(buff,0,len);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
缓冲区字符输入流BufferedReader
BufferedReader类主要从字符输入流中读取字符存入缓存区,在后面读取直接可以从缓冲区读取,不需要每次都从数据源读取并进行字符编码转换,从而提高了字符读取的效率。
package study1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Demo09 {
public static void main(String[] args) {
//缓冲区字符输入流
try (BufferedReader reader = new BufferedReader(new FileReader("F:\\Test\\IOUse\\README.md"))) {
String line = null;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
读取网页的代码
package study1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class Demo10 {
public static void main(String[] args) {
try (
//字节输入
InputStream in = new URL("https://www.163.com/")
.openConnection().getInputStream();
//字符输入流(转换流)
InputStreamReader reader = new InputStreamReader(in,"utf-8");
//缓冲字符输入流
BufferedReader br = new BufferedReader(reader);) {
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
执行ipconfig命令,并获取执行结果
package study1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Demo11 {
public static void main(String[] args) {
//执行ipconfig命令,并获取执行结果
//创建Runtime对象,用于执行命令
Runtime runtime = Runtime.getRuntime();
try {
// 执行
Process process = runtime.exec("ipconfig");
// 获取执行结果(输入流)
try(InputStream in = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));){
String line = null;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行结果:
缓冲区字符输出流BufferedWriter
BufferedWriter:将文本写入字符流,缓冲字符,以便有效的写入单个字符,数组和字符串。(可以指定缓冲区大小,或者可以接受默认大小)
写入文本文件SNxx(SN1000~SN1078)
package study1;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Demo12 {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(
new FileWriter("F:\\Test\\IOUse\\SN.txt",true));){
for(int i = 1000;i <= 1078; i++) {
String code = String.format("SN%d", i);
bw.write(code); // 写入文本文件
bw.newLine(); // 换行
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
写入ping www.163.com
补充:
在Java中,Tiemr类是用于调度后台任务的工具。它允许安排一个任务在将来的某个时间点运行,也可以按照某个固定的时间间隔重复运行任务。
package study1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalTime;
import java.util.Timer;
import java.util.TimerTask;
public class Demo13 {
public static void main(String[] args) {
System.out.printf("%s心跳检查程序启动......\n",LocalTime.now());
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec("ping www.163.com");
try(BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Test\\IoUse\\log.txt",true));){
String line = null;
while((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}, 1000, 2000);
}
}
运行结果: