Java_IO

字节流

FileOutputStream fos = new FileOutputStream("D:\\java.txt");
FileOutputStream fos = new FileOutputStream("D:\\java.txt",true);//追加写
FileOutputStream fos = new FileOutputStream(new File("D:\\java.txt"));
fos.write(98);//写字节数据
byte[] bys = {97,98,99,100,101};
byte[] bys = "abcde".getBytes();//返回字符串对应的字节数组
fos.write(bys);
fos.write(bys,2,3);//从bys数组中索引为2的元素开始,写入3个
//不同OS对换行的识别不一样,windows:\r\n,linux:\n,mac:\r
fos.write("\r\n".getBytes());
fos.close();// 关闭此文件输出流并释放与此流相关联的任何系统资源
FileInputStream fis = new FileInputStream("D:\\java.txt");
int by;
while((by = fis.read()) != -1){System.out.print((char)by);}
//一次读一个数组
byte[] bys = new byte[1024];
int len;//实际读取的数据的个数
while((len = fis.read(bys)) != -1){System.out.print(new String(bys,0,len));}

缓冲字节流

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("xxxx"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("xxxxx"));
// 字节缓冲流, 减少系统调用, 提高读写效率
byte[] bys = new byte[1024];
int len;
while((len = bis.read(bys)) != -1){bos.write(bys,0,len);}
bos.close();bis.close();

标准输出流,System.out的类型

Scanner scanner = new Scanner(System.in);
String msg = scanner.next();
// 标准输出流,改变指向位置
PrintStream out = new PrintStream(new FileOutputStream("log.txt",true));
System.setOut(out);

Date nowTime = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String strTime = simpleDateFormat.format(nowTime);
System.out.println(strTime + ": " + msg);

在这里插入图片描述

"C:\\Users\\hhx\\Downloads\\wallhaven-e71v2l.jpg"//已经写成这样的形式,仍然报错
// C:\\那里有隐藏空白字符, 删掉那块重新写即可

字符流

//读
InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\java.txt"));
char[] chs = new char[1024];//字符数组
int len;
while((len = isr.read(chs)) != -1){System.out.println(new String(chs,0,len));}
//写
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\java.txt"));
osw.write("中再次被封为sfiof",1,3);

缓冲字符流

BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\java.txt"));
for(String s : array){
    bw.write(s);
    bw.newLine();//特有方法,写分隔符(\n,\r\n,\r之类的)
}
bw.close();

BufferedReader br = new BufferedReader(new FileReader("D:\\java.txt"));
String line;
//readline是缓冲流特有方法,一次读一行,结尾的换行符什么的不会读取
while((line = br.readLine()) != null){System.out.println(line);}
br.close();

编码与解码

编码:按照某种规则,将字符存储到计算机中。
解码:将存储在计算机中的二进制数按照某种规则解析展示出来。
采用何种规则编码,就要采用对应规则解码。

// 编码
byte[] getBytes()
byte[] getBytes(String charsetName)//使用指定字符集将String编码为一系列字节,存于字节数组中
// 解码
String(byte[] bytes)
String(byte[] bytes,String charsetName)
// 编码
String s = "中国";
byte[] bys = s.getBytes();// [-28, -72, -83, -27, -101, -67]
byte[] bys = s.getBytes("GBK");// [-42, -48, -71, -6]
System.out.println(Arrays.toString(bys));
// 解码
byte[] bys = s.getBytes("GBK");// [-42, -48, -71, -6]
String ss = new String(bys,"GBK");//将字节数组解码为字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值