600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > java byte[] 文件流 转换成string是乱码_Java学习--IO(二) 多线程

java byte[] 文件流 转换成string是乱码_Java学习--IO(二) 多线程

时间:2023-05-05 09:56:46

相关推荐

java byte[] 文件流 转换成string是乱码_Java学习--IO(二) 多线程

1.标准输入流

标准输入流是指从标准输入设备流向程序的数据。

Java利用http://System.in来得到一个InputStream字节输入流

public static void main(String[] args) throws IOException {// 需求:输入一句话,然原样输出InputStream in = System.in;byte[] buf = new byte[1024];int len;// buf中包含回车和换行len = in.read(buf);String str = new String(buf, 0, len);// System.out.println(Arrays.toString(buf));System.out.println(str);}

注意:从控制台输入的字符,将会以默认编码编码成字节,进而进入输入流。同时,从控制台输入的字节将会包含换行及回车的编码。

从控制台高效读取一首诗,并将这首诗写入文件中

public static void main(String[] args) throws IOException {// 需求:从控制台高效读取一行数据。把一首诗写入文件。InputStream in = System.in;InputStreamReader reader = new InputStreamReader(in, "GBK");BufferedReader br = new BufferedReader(reader);File file = new File("d:javatestk.txt");FileWriter writer = new FileWriter(file);BufferedWriter bw = new BufferedWriter(writer);String end = "bye";while(true) {String line = br.readLine();if(line.equals(end)) {break;}bw.write(line);// bw.newLine();}bw.flush();bw.close();writer.close();}

2.标准输出流(PrintStream)

标准输出流是流向标准输出设备(显示器)的数据。

Java中用System.out得到PrintStream字节输出流(字节打印流)。

含:

print

println方法

public static void main(String[] args) throws IOException {File file = new File("d:javatestk.txt");FileReader reader = new FileReader(file);BufferedReader br = new BufferedReader(reader);PrintStream ps = System.out;String line;while( (line=br.readLine())!=null ) {ps.println(line);}}

注意:PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节

public static void main(String[] args) throws IOException {String str = "hello中国";byte[] buf = str.getBytes("utf-8");PrintStream ps = System.out;ps.write(buf);}

以上将会发生乱码。

3.字符打印流PrintWriter

实现了PrintStream中所有的方法,同样是标准输出流是流向标准输出设备(显示器)的数据。但是是输出格式化的表示形式。

此类方法不会抛出IO异常。

此方法可以按照一定的格式化写入到我们的文档当中。

public static void main(String[] args) throws IOException {File file = new File("g:javatesta.txt");PrintWriter pw = new PrintWriter(file);Date date = new Date(0);pw.format("%tA", date);pw.flush();pw.close();}

这里给出一些常用日期格式化转换符如下:

4.Scanner类

用于扫描文件、控制台、字节流等等。

public static void main(String[] args) throws IOException {// 扫描平台默认编码的文件/*File file = new File("d:javatestj.txt");Scanner sc = new Scanner(file);*/// 扫描指定编码的文件Scanner sc = new Scanner(new FileInputStream(new File("d:javatestj-utf8.txt")), "UTF-8");String line;while (sc.hasNextLine()) {line = sc.nextLine();System.out.println(line);}}

5.序列化

把内存中的对象永久保存到硬盘的过程称为对象序列化,也叫做持久化。

把硬盘持久化的内存恢复的内存的过程称为对象反序列化。

5.1 Serializable

Serializable接口没有方法或字段,仅用于标识可序列化的语义,类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化,并抛出异常。

5.1.1序列化对象

ObjectOutputStream 继承于OutputStream,专门用于把对象序列化到本地。

public static void main(String[] args) throws IOException {Student stu = new Student("001", "大狗", 20, Gender.男);/***方案1:取stu所有的属性,通过特定的字符串(-),把各个属性值连接起来* 001-大狗-20-男*/File file = new File("d:javatestl.txt");FileOutputStream out = new FileOutputStream(file);ObjectOutputStream oos = new ObjectOutputStream(out);oos.writeObject(stu);oos.close();out.close();}

5.1.2反序列化对象

ObjectInputStream 继承于InputStream ,专门用于把本地持久化内容反序列化到内存。

public static void main(String[] args) throws IOException, ClassNotFoundException {File file = new File("d:javatestl.txt");FileInputStream in = new FileInputStream(file);ObjectInputStream ois = new ObjectInputStream(in);Student student = (Student) ois.readObject();System.out.println(student.getId());System.out.println(student.getName());System.out.println(student.getAge());System.out.println(student.getGender());ois.close();in.close();}

5.2 transient关键字

开发过程中,如果想忽略某些字段不让其序列化时,可以使用transient修饰。

public class Student implements Serializable {private static final long serialVersionUID = 7222966748321328300L;private String id;private transient String name;private transient int age;private Gender gender;private String phone;

6. DataInputStream / DataOutputStream

这两者分别继承于InputStream与OutputStream。特别的是他们很适合读取/写入在网络传输过程中的数据流。

public static void main(String[] args) throws IOException {File file = new File("d:javatestn.txt");FileOutputStream out= new FileOutputStream(file);DataOutputStream dos = new DataOutputStream(out);dos.writeInt(10);dos.writeUTF("hello中国");dos.close();out.close();System.out.println("写入完成");}

public static void main(String[] args) throws IOException {File file = new File("d:javatestn.txt");FileInputStream in = new FileInputStream(file);DataInputStream dis = new DataInputStream(in);int a = dis.readInt();String str = dis.readUTF();System.out.println(a);System.out.println(str);}

7.多线程

7.1程序与进程

在计算机刚出现时,计算机的效率是十分低下的,尤其是对CPU的利用率,当一个程序运行时,将会产生一段进程,计算机需把该进程全部完成,才能让下一段程序进入,这样将会对效率产生极大的影响。

对于当今电脑来讲,已经不存在着这样的问题了,因为电脑引入了进程与多线程的概念,使得在宏观上表现出了cpu在同时处理多个程序,但在微观上,即实际上,cpu每次都只能运行一个程序。由于速度较快,人类感受不到这之间的差距。较为明显的感受即是,电脑发生卡顿。此时说明有一个程序正在CPU中运行,且无法被移出或短时间内无法移出。

7.2进程与线程的区别

7.3实现多线程

第一种:

public class MyThread extends Thread {@Overridepublic void run() {System.out.println("我是多线程MyThread");for (int i = 0; i < 5; i++) {System.out.println("MyThread:" + i);}}}public class Test01 {public static void main(String[] args) {// main开始运行产生一个进程,该进程默认有个主(main)线程// 创建线程MyThread t1 = new MyThread();// 启动线程t1.start();for (int i = 0; i < 5; i++) {System.out.println("main Thread:" + i);}}

第二种:

public class MyRun implements Runnable {@Overridepublic void run() {System.out.println("我是MyRun");for (int i = 0; i < 5; i++) {System.out.println("my run:" + i);}}}public class Test02 {public static void main(String[] args) {MyRun run = new MyRun();Thread t1 = new Thread(run);t1.start();// main开始运行产生一个进程,该进程默认有个主(main)线程for (int i = 0; i < 5; i++) {System.out.println("main Thread:" + i);}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。