IO
编码、解码、乱码
public class Test2 {
public static void main(String[] args) throws UnsupportedEncodingException {
// 1.编码:字符串变成 byte[]
String s = "aW接大"
byte[] bytes = s.getBytes()
System.out.println(Arrays.toString(bytes))
byte[] bytes1 = s.getBytes("GBK")
System.out.println(Arrays.toString(bytes1))
// 2. 解码:byte[] 变成字符串
String s1 = new String(bytes)
System.out.println(s1)
String s2 = new String(bytes1, "GBK")
System.out.println(s2)
// 3. 乱码:编码方式和解码方式不同,会导致乱码 (如果只有英文或数字,不会乱码)
String s3 = new String(bytes, "GBK")
System.out.println(s3)
String s4 = new String(bytes1)
System.out.println(s4)
}
}
FileInputStream读内容
public class FileInputStreamTest2 {
public static void main(String[] args) throws IOException {
FileInputStream is = new FileInputStream("day08\src\com\itheima\d4_byte_stream\itheima01.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
String s = new String(buffer, 0, len);
System.out.println(s);
}
is.close();
}
}
FileOutputStream写内容
public class FileOutputStreamTest4 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("day08\src\com\itheima\d4_byte_stream\itheima02.txt", true);
fos.write('W');
fos.write('E');
fos.write('R');
String s = "今晚奖励自己一顿大餐";
fos.write(s.getBytes());
fos.write(s.getBytes(), 0, 10);
fos.write("\n\r".getBytes());
fos.close();
}
}
复制图片(基于字节流)
try-with-resource
public class Test3 {
public static void main(String[] args) {
try (
FileInputStream is = new FileInputStream("day08\src\com\itheima\d5_resource\qqq.txt");
FileOutputStream fos = new FileOutputStream("day08\src\com\itheima\d5_resource\qqq2.txt");
) {
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
try-catch-finally
public class Test {
public static void main(String[] args) {
FileInputStream is = null;
FileOutputStream fos = null;
try {
is = new FileInputStream("day08\src\com\itheima\d5_resource\qqq.txt");
fos = new FileOutputStream("day08\src\com\itheima\d5_resource\qqq2.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
读含汉字文件(try-with-resource写法)
public class FileReaderTest1 {
public static void main(String[] args) {
try (
FileReader reader = new FileReader("day09\src\itheima\d1_char_stream\abc.txt");
) {
char[] buffer = new char[1024];
int len;
while ((len = reader.read(buffer)) != -1) {
String s = new String(buffer, 0, len);
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
写含汉字文件(try-catch-finally)
public class FileWriterTest2 {
public static void main(String[] args) {
FileWriter writer = null;
try {
writer = new FileWriter("day09\src\itheima\d1_char_stream\novel.txt", true);
writer.write('A');
writer.write('龙');
writer.write("我TM写写写写写");
String s = "sh龙dbaidbi阿萨德吧上课的按时等不及哈巴三大";
writer.write(s, 0, 5);
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
复制文本(基于字符流)
public class FileReadWriterCopy {
public static void main(String[] args) {
try (
FileReader reader = new FileReader("day09\src\itheima\d1_char_stream\doupo.txt");
FileWriter writer = new FileWriter("day09\src\itheima\d1_char_stream\doupo_copy.txt");
) {
char[] buffer = new char[1024];
int len;
while ((len = reader.read(buffer)) != -1) {
writer.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
字节缓冲流
public class BufferedInputStreamTest1 {
public static void main(String[] args) {
FileInputStream is = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
is = new FileInputStream("day09\src\itheima\d2_buffered_stream\cat.jpg");
bis = new BufferedInputStream(is);
fos = new FileOutputStream("day09\src\itheima\d2_buffered_stream\cat_copy.jpg");
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
字符缓冲流
public class BufferedReaderTest2 {
public static void main(String[] args) {
try (
// 掌握字符缓冲输入流的用法。
FileReader reader = new FileReader("day09\src\itheima\d2_buffered_stream\doupo.txt")
BufferedReader br = new BufferedReader(reader)
) {
/*
经典写法:一次读1024个字符
char[] buffer = new char[1024]
int len
while ((len = br.read(buffer)) != -1) {
String s = new String(buffer, 0, len)
System.out.println(s)
}*/
// BufferedReader独有方法:一次读一行
String line
while ((line = br.readLine()) != null) {
System.out.println(line)
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
public class BufferedWriterTest3 {
public static void main(String[] args) {
try (
FileWriter writer = new FileWriter("day09\src\itheima\d2_buffered_stream\doupo01.txt", true);
BufferedWriter bw = new BufferedWriter(writer);
) {
bw.write(",有一个小镇青年,叫萧炎");
bw.newLine();
bw.write("后续内容收费");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
转换输入流
public class InputStreamReaderTest1 {
public static void main(String[] args) {
try (
FileInputStream is = new FileInputStream("day09\src\itheima\d3_transform_stream\panlong.txt");
InputStreamReader isr = new InputStreamReader(is,"GBK");
BufferedReader br = new BufferedReader(isr);
) {
String s;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
转换输出流
public class InputStreamReaderTest2 {
public static void main(String[] args) throws IOException {
try (
FileOutputStream fos = new FileOutputStream("day09\src\itheima\d3_transform_stream\panlong.txt", true);
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
BufferedWriter bw = new BufferedWriter(osw);
) {
bw.write(",刹那间,村门口来了一位穿着背带裤的青年,发出桀桀桀的声音");
} catch (Exception e) {
e.printStackTrace();
}
}
}
序列化流
对象
public class User implements Serializable {
private static final long serialVersionUID = -5294956369762487124L;
private String loginName;
private transient String password;
private String userName;
private int age;
}
输出
public class Test1ObjectOutputStream {
public static void main(String[] args) {
try (
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day09\src\itheima\d6_object_stream\user.txt"));
) {
User user = new User("root", "123456", "张三", 18);
System.out.println(user);
oos.writeObject(user);
} catch (Exception e) {
e.printStackTrace();
}
}
}
读取
public class Test2ObjectInputStream {
public static void main(String[] args) {
ObjectInputStream dis = null;
try {
dis = new ObjectInputStream(new FileInputStream("day09\src\itheima\d6_object_stream\user.txt"));
User user = (User) dis.readObject();
user.setAge(100);
System.out.println(user);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(dis);
}
}
}
CommonsIO
package itheima.d7_commons_io;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.IOException;
public class CommonsIOTest1 {
public static void main(String[] args) throws IOException {
FileUtils.copyFile(new File("day09\src\itheima\d7_commons_io\cat.jpg"), new File("day09\src\itheima\d7_commons_io\cat_copy.jpg"));
FileUtils.copyDirectory(new File("day09\src\itheima\d7_commons_io\d1"), new File("day09\src\itheima\d7_commons_io\d1_copy"));
FileUtils.deleteDirectory(new File("day09\src\itheima\d7_commons_io\d1_copy"));
IOUtils.closeQuietly();
}
}