Skip to content

Commit 892fcd4

Browse files
committed
文件读写
1 parent 0e0c308 commit 892fcd4

File tree

9 files changed

+227
-2
lines changed

9 files changed

+227
-2
lines changed

fileInputStream.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
2+
读文件
3+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
4+
读文件
5+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
6+
读文件
7+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
8+
读文件
9+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
10+
读文件
11+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
12+
读文件
13+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
14+
读文件
15+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
16+
读文件
17+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
18+
读文件
19+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
20+
读文件
21+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
22+
读文件
23+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
24+
读文件
25+
hello world!hello world!hello world!hello world!hello world!hello world!hello world!hello world!
26+
读文件

fileOutputStream.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hello world! 写文件
2+
写文件 hello world!
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.code.repository.charset;
2+
3+
/**
4+
* char 和 byte的关系
5+
* @author hm
6+
*
7+
*/
8+
public class CharAndByteDemo {
9+
10+
public static void main(String[] args) {
11+
char a = 'a';
12+
byte[] b = CharAndByteDemo.charToByte(a);
13+
System.out.println(b[0]);
14+
System.out.println(b[1]);
15+
}
16+
17+
/**
18+
* 字符char转换成byte
19+
*
20+
* 首先要明白的是:
21+
* byte是字节数据类型,由8位二进制组成;
22+
* char是字符数据类型,占2个字节byte;
23+
*
24+
* @param c
25+
* @return
26+
*/
27+
public static byte[] charToByte(char c) {
28+
byte[] b = new byte[2];// 一个字符是2个字节,所以b的长度为2
29+
b[0] = (byte)((c & 0xFF00) >> 8);// 0xff00 为 1111 1111 0000 0000
30+
b[1] = (byte)(c & 0xff); // 0xff为 1111 1111
31+
return b;
32+
}
33+
34+
public static char byteToChar(byte[] b) {
35+
char c = (char)(((b[0] & 0xff) << 8)|(b[1] & 0xff));
36+
return c;
37+
}
38+
}

src/main/java/com/code/repository/charset/StringAndChar.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import java.io.UnsupportedEncodingException;
44

5+
/**
6+
* String 内部默认utf-16编码,unicode字符集,2字节;
7+
* 用char数组存储,一个char占2个字节;
8+
* String.length返回的是char数组的长度
9+
* @author hm
10+
*
11+
*/
512
public class StringAndChar {
613

714
static String GBK = "GBK";
@@ -15,16 +22,23 @@ public class StringAndChar {
1522
public static void main(String[] args) throws UnsupportedEncodingException {
1623
String str = new String("中"); // 汉字,GBK、unicode是2个字节;utf-8是3个字节,文件默认是UTF-8编码
1724

25+
System.out.println("default:"+str.getBytes().length);// 默认是utf-8,输出3
26+
27+
System.out.println("change ot GBK:"+new String (str.getBytes("GBK"),"GBK"));
28+
1829
byte[] bit_utf_8 = str.getBytes(UTF8);
1930
System.out.println("utf-8:"+bit_utf_8.length);// 输出字节长度,utf-8编码多字节编码,汉字是3个,所以输出3
2031

2132
for(byte b : bit_utf_8) {
2233
System.out.println( Integer.toBinaryString((b & 0xFF) + 0x100).substring(1));
2334
}
2435

25-
// 把这个utf-8的字节转换成string
36+
37+
/**
38+
* 把这个utf-8的字节转换成string,3个字节变成2个字节,看源码实现
39+
*/
2640
String strFromUtf8 = new String(bit_utf_8);
27-
System.out.println("strFromUtf8:"+strFromUtf8 +" length:"+strFromUtf8.length());
41+
System.out.println("strFromUtf8:"+strFromUtf8 +" length:"+strFromUtf8.length());// String 内部默认utf-16编码,用char数组存储,一个char占2个字节,输出的长度为数组长度,所以为1
2842

2943
byte[] bit_GBK = str.getBytes(GBK);
3044
System.out.println("gbk:"+bit_GBK.length);// 输出字节长度,gbk编码2个字节编码,所以输出2
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.code.repository.io;
2+
3+
public class AutoColseableTest {
4+
5+
public static void main(String[] args) {
6+
try(MyResource my = new MyResource()) {
7+
my.invoke();
8+
} catch (Exception e) {
9+
}
10+
System.out.println("end");
11+
}
12+
13+
// 资源自动关闭的接口AutoCloseable
14+
// 在使用的时候只需要把资源在try块中用小括号括起来就可以了
15+
static class MyResource implements AutoCloseable{
16+
@Override
17+
public void close() throws Exception {
18+
System.out.println("i am closed!");
19+
20+
}
21+
22+
public void invoke() {
23+
System.out.println("i am working!");
24+
}
25+
}
26+
27+
}
28+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.code.repository.io;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
6+
/**
7+
*
8+
* @author hm
9+
*
10+
*/
11+
public class FileInputStreamTest {
12+
13+
public static void main(String[] args) throws IOException {
14+
FileInputStream fis = null;
15+
try {
16+
// 创建字节文件输入流
17+
fis = new FileInputStream("fileInputStream.txt");
18+
// 最多读64字节
19+
byte[] b = new byte[64];
20+
// 实际读取字节数
21+
int len = 0;
22+
// 循环读取并输出
23+
while((len=fis.read(b)) > 0) {
24+
System.out.print(new String(b,0,len)+"|");//注意: 读取后,对中文转换有乱码问题
25+
}
26+
} catch (Exception e) {
27+
e.printStackTrace();
28+
} finally {
29+
fis.close();
30+
}
31+
32+
}
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.code.repository.io;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
7+
public class FileOutputStreamTest {
8+
9+
public static void main(String[] args) throws IOException {
10+
FileOutputStream fos = null;
11+
12+
try {
13+
fos = new FileOutputStream("fileOutputStream.txt");
14+
String str = "hello world! 写文件";
15+
String str2 = "写文件 hello world! ";
16+
fos.write(str.getBytes());
17+
fos.write('\n');
18+
fos.write(str2.getBytes());
19+
System.out.println("ok");
20+
} catch (FileNotFoundException e) {
21+
// TODO Auto-generated catch block
22+
e.printStackTrace();
23+
} finally {
24+
fos.close();
25+
}
26+
27+
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.code.repository.io;
2+
3+
import java.io.FileReader;
4+
import java.io.IOException;
5+
6+
public class FileReaderTest {
7+
8+
public static void main(String[] args) throws IOException {
9+
FileReader fr = null;
10+
try{
11+
// 创建字符文件输入流
12+
fr = new FileReader("fileInputStream.txt");
13+
// 每次读取128个字符
14+
char[] b = new char[128];
15+
int len;
16+
// 循环读取
17+
while((len = fr.read(b)) > 0) {
18+
System.out.print(new String(b,0,len));
19+
}
20+
}catch (Exception e) {
21+
}finally {
22+
fr.close();
23+
}
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.code.repository.io;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
8+
// 转换流
9+
public class InputStreamReaderTest {
10+
11+
public static void main(String[] args) throws IOException {
12+
// 字节流转换成reader字符流
13+
InputStreamReader reader = new InputStreamReader(new FileInputStream("fileInputStream.txt"));
14+
// 包装成reader字符缓冲流
15+
BufferedReader br = new BufferedReader(reader);
16+
String str = null;
17+
18+
try {
19+
// 读取每一行打印
20+
while((str = br.readLine())!=null) {
21+
System.out.println(str);
22+
}
23+
} catch (IOException e) {
24+
// TODO Auto-generated catch block
25+
e.printStackTrace();
26+
} finally {
27+
br.close();
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)