目录
IO入门
文件编码与解码
字符集:Java字符使用16位的双字节存储,实际文件存储的数据有各种字符集。
编码(encode):字符 —》字节。
解码(decode):字节 —》字符。
不同的编码方式对应不同的字符集
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
public class TestEncode {
public static void main(String[] args) throws UnsupportedEncodingException { //throws抛出异常
String msg = "性命生命使命";
byte[] datas = msg.getBytes(); //默认是utf8
System.out.println(datas.length);
//编码其他字符集
byte[] datas2 = msg.getBytes(StandardCharsets.UTF_16LE);
System.out.println(datas2.length);
//解码为字符串
String msg1 = new String(datas,0,datas.length,"utf8");
System.out.println(msg1);
//解码为字符串 (字符集与编码方式不对应,乱码)
String msg2 = new String(datas,0,datas.length,"UTF_16LE");
System.out.println(msg2);
}
}
运行结果:
四个抽象类
字节流和字符流
注:(1)close() 是通知 操作系统 可以去释放文件了。 (2)flush()是强制输出缓冲里的东西。
文件字节输入流
不规范代码:
import java.io.*;
public class IOTest01 {
public static void main(String[] args) throws IOException {
File src = new File("D:/Java/IO/abc.txt"); // 1.创建源
InputStream is = new FileInputStream(src); // 2.选择流
int data1 = is.read(); // 3.操作
int data2 = is.read();
System.out.println(data1);
System.out.println((char)data1);
System.out.println((char)data2);
is.close(); // 4.释放资源
}
}
运行结果:
较为规范代码:
import java.io.*;
public class IOTest02 {
public static void main(String[] args) throws IOException {
File src = new File("D:/Java/IO/abc.txt"); // 1.创建源
InputStream is = new FileInputStream(src); // 2.选择流
int temp;
while((temp=is.read()) != -1) {//编译器里空的是-1
System.out.print((char)temp); // 3.操作
}
if(null!=is){
is.close(); // 4.释放资源
}
}
}
正式规范代码
import java.io.*;
public class IOTest03 {
public static void main(String[] args) {
File src = new File("D:/Java/IO/abc.txt"); // 创建源
InputStream is = null;
try{
is = new FileInputStream(src); // 选择流
int temp;
while((temp=is.read())!=-1){
System.out.print((char)temp);
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
try{
if(null!=is){
is.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}
(这个代码是读一个,打一个)
(下面的代码是读一段,打一段)
import java.io.*;
public class IOTest03 {
public static void main(String[] args) {
File src = new File("D:/Java/IO/abc.txt");
InputStream is = null;
try{
is = new FileInputStream(src); //选择流
byte[] flush = new byte[3]; //缓冲容器,常用1024
int len = -1; //接受长度
while((len=is.read(flush))!=-1){
// 字节数组—》字符串
String str = new String(flush,0,len);
System.out.println(str);
}
} catch(IOException e){
e.printStackTrace();
} finally {
try{
if(null!=is){
is.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}
文件字节输出流
import java.io.*;
public class IOTest04 {
public static void main(String[] args) {
File src = new File("D:/Java/IO/test.txt"); //创建源
OutputStream os = null; //选择流
try{
os = new FileOutputStream(src); //默认是false重新写,如果是ture,则是追屁股写
String msg = "IO is easy";
byte[] datas = msg.getBytes(); //编码
os.write(datas,0,datas.length);
os.flush(); //养成刷新的习惯
}catch (IOException e){
e.printStackTrace();
}finally {
try{
if(null!=os){
os.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}