日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

發(fā)布時(shí)間:2024/8/23 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java byte[] 文件流 转换成string是乱码_Java学习--IO(二)、多线程 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.標(biāo)準(zhǔn)輸入流

標(biāo)準(zhǔn)輸入流是指從標(biāo)準(zhǔn)輸入設(shè)備流向程序的數(shù)據(jù)。

Java利用http://System.in來(lái)得到一個(gè)InputStream字節(jié)輸入流

public static void main(String[] args) throws IOException {// 需求:輸入一句話,然原樣輸出InputStream in = System.in;byte[] buf = new byte[1024];int len;// buf中包含回車和換行l(wèi)en = in.read(buf);String str = new String(buf, 0, len);// System.out.println(Arrays.toString(buf));System.out.println(str);}

注意:從控制臺(tái)輸入的字符,將會(huì)以默認(rèn)編碼編碼成字節(jié),進(jìn)而進(jìn)入輸入流。同時(shí),從控制臺(tái)輸入的字節(jié)將會(huì)包含換行及回車的編碼。

從控制臺(tái)高效讀取一首詩(shī),并將這首詩(shī)寫入文件中

public static void main(String[] args) throws IOException {// 需求:從控制臺(tái)高效讀取一行數(shù)據(jù)。把一首詩(shī)寫入文件。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.標(biāo)準(zhǔn)輸出流(PrintStream)

標(biāo)準(zhǔn)輸出流是流向標(biāo)準(zhǔn)輸出設(shè)備(顯示器)的數(shù)據(jù)。

Java中用System.out得到PrintStream字節(jié)輸出流(字節(jié)打印流)。

含:

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 打印的所有字符都使用平臺(tái)的默認(rèn)字符編碼轉(zhuǎn)換為字節(jié)

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

以上將會(huì)發(fā)生亂碼。

3.字符打印流PrintWriter

實(shí)現(xiàn)了PrintStream中所有的方法,同樣是標(biāo)準(zhǔn)輸出流是流向標(biāo)準(zhǔn)輸出設(shè)備(顯示器)的數(shù)據(jù)。但是是輸出格式化的表示形式。

此類方法不會(huì)拋出IO異常。

此方法可以按照一定的格式化寫入到我們的文檔當(dāng)中。

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();}

這里給出一些常用日期格式化轉(zhuǎn)換符如下:

4.Scanner類

用于掃描文件、控制臺(tái)、字節(jié)流等等。

public static void main(String[] args) throws IOException {// 掃描平臺(tái)默認(rèn)編碼的文件/*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.序列化

把內(nèi)存中的對(duì)象永久保存到硬盤的過(guò)程稱為對(duì)象序列化,也叫做持久化。

把硬盤持久化的內(nèi)存恢復(fù)的內(nèi)存的過(guò)程稱為對(duì)象反序列化。

5.1 Serializable

Serializable接口沒有方法或字段,僅用于標(biāo)識(shí)可序列化的語(yǔ)義,類通過(guò)實(shí)現(xiàn) java.io.Serializable 接口以啟用其序列化功能。未實(shí)現(xiàn)此接口的類將無(wú)法使其任何狀態(tài)序列化或反序列化,并拋出異常。

5.1.1序列化對(duì)象

ObjectOutputStream 繼承于OutputStream,專門用于把對(duì)象序列化到本地。

public static void main(String[] args) throws IOException {Student stu = new Student("001", "大狗", 20, Gender.男);/*** 方案1:取stu所有的屬性,通過(guò)特定的字符串(-),把各個(gè)屬性值連接起來(lái)* 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反序列化對(duì)象

ObjectInputStream 繼承于InputStream ,專門用于把本地持久化內(nèi)容反序列化到內(nèi)存。

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關(guān)鍵字

開發(fā)過(guò)程中,如果想忽略某些字段不讓其序列化時(shí),可以使用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。特別的是他們很適合讀取/寫入在網(wǎng)絡(luò)傳輸過(guò)程中的數(shù)據(jù)流。

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中國(guó)");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程序與進(jìn)程

在計(jì)算機(jī)剛出現(xiàn)時(shí),計(jì)算機(jī)的效率是十分低下的,尤其是對(duì)CPU的利用率,當(dāng)一個(gè)程序運(yùn)行時(shí),將會(huì)產(chǎn)生一段進(jìn)程,計(jì)算機(jī)需把該進(jìn)程全部完成,才能讓下一段程序進(jìn)入,這樣將會(huì)對(duì)效率產(chǎn)生極大的影響。

對(duì)于當(dāng)今電腦來(lái)講,已經(jīng)不存在著這樣的問題了,因?yàn)殡娔X引入了進(jìn)程與多線程的概念,使得在宏觀上表現(xiàn)出了cpu在同時(shí)處理多個(gè)程序,但在微觀上,即實(shí)際上,cpu每次都只能運(yùn)行一個(gè)程序。由于速度較快,人類感受不到這之間的差距。較為明顯的感受即是,電腦發(fā)生卡頓。此時(shí)說(shuō)明有一個(gè)程序正在CPU中運(yùn)行,且無(wú)法被移出或短時(shí)間內(nèi)無(wú)法移出。

7.2進(jìn)程與線程的區(qū)別

7.3實(shí)現(xiàn)多線程

第一種:

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開始運(yùn)行產(chǎn)生一個(gè)進(jìn)程,該進(jìn)程默認(rèn)有個(gè)主(main)線程// 創(chuàng)建線程MyThread t1 = new MyThread();// 啟動(dòng)線程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開始運(yùn)行產(chǎn)生一個(gè)進(jìn)程,該進(jìn)程默認(rèn)有個(gè)主(main)線程for (int i = 0; i < 5; i++) {System.out.println("main Thread:" + i);}} }

總結(jié)

以上是生活随笔為你收集整理的java byte[] 文件流 转换成string是乱码_Java学习--IO(二)、多线程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。