文章目录
数据流简介
- 数据流简介
- OutputStream
- 简介
- 常用方法
- FileOutputStream子类
- 实现数据的追加
- InputStream字节输入流
- 简介
- 常用方法
- 数据读取
文件输出
import java.io.*;
public class YootkDemo { // 李兴华编程训练营:yootk.ke.qq.com
public static void main(String[] args) throws Exception {
File file = new File("H:" + File.separator + "muyan" + File.separator + "vip" + File.separator + "yootk.txt") ;
if (!file.getParentFile().exists()) { // 此时文件有父目录
file.getParentFile().mkdirs() ; // 创建父目录
}
OutputStream output = new FileOutputStream(file) ; // 实例化OutputStream抽象类对象
String message = "www.yootk.com" ; // 此为要输出的数据内容
// OutputStream类的输出是以字节数据类型为主的,所以需要将字符串转为字节数据类型
byte data [] = message.getBytes() ; // 将字符串转为字节数句
output.write(data); // 输出全部字节数组的内容
output.close(); // 关闭输出流
}
}
import java.io.*;
public class YootkDemo { // 李兴华编程训练营:yootk.ke.qq.com
public static void main(String[] args) throws Exception {
File file = new File("H:" + File.separator + "muyan" + File.separator + "vip" + File.separator + "yootk.txt") ;
if (!file.getParentFile().exists()) { // 此时文件有父目录
file.getParentFile().mkdirs() ; // 创建父目录
}
OutputStream output = new FileOutputStream(file, true) ; // 实例化OutputStream抽象类对象
String message = "www.yootk.com\r\n" ; // 此为要输出的数据内容
// OutputStream类的输出是以字节数据类型为主的,所以需要将字符串转为字节数据类型
byte data [] = message.getBytes() ; // 将字符串转为字节数句
output.write(data); // 输出全部字节数组的内容
output.close(); // 关闭输出流
}
}
InputStream字节输入流
简介
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class YootkDemo { // 李兴华编程训练营:yootk.ke.qq.com
public static void main(String[] args) throws Exception {
File file = new File("H:" + File.separator + "muyan" + File.separator + "vip" + File.separator + "yootk.txt");
if (file.exists()) { // 文件存在
try (InputStream input = new FileInputStream(file)) {
byte data[] = new byte[1024]; // 开辟1K的空间进行读取
int len = input.read(data); // 读取数据到字节数组并返回读取个数
System.out.println("读取到的数据内容【" + new String(data, 0, len) + "】");
} catch (Exception e) {}
}
}
}