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字節輸出流(字節打印流)。
含:
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);}} }總結
以上是生活随笔為你收集整理的java byte[] 文件流 转换成string是乱码_Java学习--IO(二)、多线程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IntelliJ IDEA的这个接口调试
- 下一篇: 第一次失效_特斯拉螺栓腐蚀失效分析_搜狐