IO(字节流)
public class ByteStreamDemo1 {
public static void main(String[] args) throws IOException {
/*
字节输出流 FileOutputStream
需求:写出一段文字到指定文件中
步骤:1.创建对象
细节1:参数是字符串表示的路径或者File对象
细节2:如果文件不存在,会自动创建,但是父级路径必须存在
细节3:如果文件存在,会清空文件
2.写出数据
细节:参数是整数但是写入的是对应的字符
3.释放资源
每次使用完流之后都要释放资源
*/
//1.创建对象
FileOutputStream fos=new FileOutputStream("day31-code\\src\\a.txt");
//2.写出数据
fos.write(72);
fos.write(65);
fos.write(72);
fos.write(65);
//3.释放资源
fos.close();
}
}
------------------------------
public class ByteStreamDemo2 {
public static void main(String[] args) throws IOException {
/*
write(int b)
write(byte[] b)
write(byte[] b,int off,int len)
参数二:起始索引
参数三:写出的个数
*/
FileOutputStream fos=new FileOutputStream("day31-code\\src\\a.txt");
fos.write(72);
byte[] bytes={65,72,65};
fos.write(bytes);
byte[] bytes1={65,72,65,72,65,72};
fos.write(bytes1,1,4);
fos.close();
}
}
----------------------------
public class ByteStreamDemo3 {
public static void main(String[] args) throws IOException {
/*
换行:
windows: \r\n
续写:
想要续写就要打开续写开关,即在构造方法中传入第二个参数true
默认是false,不开启续写
*/
FileOutputStream fos=new FileOutputStream("day31-code\\src\\a.txt",true);
String str="xiangxiang";
byte[] bytes=str.getBytes();
fos.write(bytes);
fos.write("\r\n".getBytes());
String str1="666";
byte[] bytes1=str1.getBytes();
fos.write(bytes1);
fos.write("\r\n".getBytes());
fos.close();
}
}
---------------------------
public class ByteStreamDemo4 {
public static void main(String[] args) throws IOException {
/*
字节输出流 FileInputStream
需求:写出一段文字到指定文件中
步骤:1.创建对象
细节:文件不存在直接报错
2.读取数据
读出来的是ascii码值,需要强转成char类型
读到末尾方法返回-1
3.释放资源
每次使用完流之后都要释放资源
*/
//1.创建对象
FileInputStream fis=new FileInputStream("day31-code\\src\\a.txt");
//2.读取数据
char c1 = (char)fis.read();
System.out.println(c1);
char c2 = (char)fis.read();
System.out.println(c2);
char c3 = (char)fis.read();
System.out.println(c3);
char c4 = (char)fis.read();
System.out.println(c4);
char c5 = (char)fis.read();
System.out.println(c5);
//3.释放资源
fis.close();
}
}
---------------------------------
public class ByteStreamDemo5 {
public static void main(String[] args) throws IOException {
//循环读取
FileInputStream fis=new FileInputStream("day31-code\\src\\a.txt");
int b;
while((b=fis.read()) !=-1){
System.out.print((char)b);
}
fis.close();
}
}
---------------------------
public class ByteStreamDemo6 {
public static void main(String[] args) throws IOException {
//拷贝同时记录拷贝时间
long start = System.currentTimeMillis();
//1.创建对象
FileOutputStream fos=new FileOutputStream("day31-code\\src\\IO\\b.txt");
FileInputStream fis=new FileInputStream("day31-code\\src\\a.txt");
//2.拷贝
//思想:边读边写
int a;
while((a=fis.read()) !=-1){
fos.write(a);
}
long end = System.currentTimeMillis();
System.out.println("拷贝时间"+(end-start));
//3.释放资源
fos.close();
fis.close();
}
}
--------------------------
public class ByteStreamDemo7 {
public static void main(String[] args) throws IOException {
//一次读取多个字节
/*
public int read(byte[] b) 一次读取一个字节数组的数据
*/
//1.创建对象
FileInputStream fis=new FileInputStream("day31-code\\src\\a.txt");
//2.读取数据
byte[] bytes=new byte[3];//一次读3个字节,每次读取覆盖数组中的内容,不够则覆盖一部分(所以在new string()方法中应当传入起始索引和传入长度)
int len=fis.read(bytes);//返回读取字节的个数,没读取到返回-1
System.out.println(len);
String str=new String(bytes,0,len);
System.out.println(str);
int len1=fis.read(bytes);//返回读取字节的个数,没读取到返回-1
System.out.println(len1);
String str1=new String(bytes,0,len1);
System.out.println(str1);
int len2=fis.read(bytes);//返回读取字节的个数,没读取到返回-1
System.out.println(len2);
String str2=new String(bytes,0,len2);
System.out.println(str2);
//3.释放资源
fis.close();
}
}
-------------------------------------
public class ByteStreamDemo8 {
public static void main(String[] args) throws IOException {
//文件拷贝,改进版
FileInputStream fis=new FileInputStream("day31-code\\src\\a.txt");
FileOutputStream fos=new FileOutputStream("day31-code\\src\\IO\\b.txt");
//定义字节数组一次拷贝1024个字节(1KB)拷贝数据比较大时可以增加数组大小1024*1024=1MB,1024*1024*5=5MB,也不能太大
byte[] bytes=new byte[1024];
int len;
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}
}
-----------------------------
public class ByteStreamDemo9 {
public static void main(String[] args) {
//try catch finally处理异常
//不论有没有异常都会执行finally里的代码
FileInputStream fis= null;
FileOutputStream fos= null;
try {
//文件拷贝,改进版
fis = new FileInputStream("day31-code\\src\\a.txt");
fos = new FileOutputStream("day31-code\\src\\IO\\b.txt");
//定义字节数组一次拷贝1024个字节(1KB)拷贝数据比较大时可以增加数组大小1024*1024=1MB,1024*1024*5=5MB,也不能太大
byte[] bytes=new byte[1024];
int len;
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if(fis!=null){//先要判断fis是否为空,否则会出现空指针异常
try {
fis.close();//fis.close()也可能出异常
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(fos!=null){//先要判断fos是否为空,否则会出现空指针异常
try {
fos.close();//fos.close()也可能出异常
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Hexo!