12、java中的I/O流(2)
生活随笔
收集整理的這篇文章主要介紹了
12、java中的I/O流(2)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
再介紹一下其他一些流的使用
數(shù)據(jù)操作流,數(shù)據(jù)輸入流允許應(yīng)用程序以獨(dú)立于機(jī)器的方式從底層輸入流讀取原始Java數(shù)據(jù)類型,意思就是平臺無關(guān),相關(guān)的兩個類DataInputStream、DataOutputStream,使用如下:
public class DataInputStreamTest {public static void main(String[] args) {DataInputStreamTest test = new DataInputStreamTest();test.testDataOutputStream(); // test.testDataInputStream();}public void testDataInputStream() {DataInputStream dataInputStream = null;try {dataInputStream = new DataInputStream(new FileInputStream("E:\\test\\input.doc"));byte []b = new byte[1024];dataInputStream.read(b);System.out.println(new String(b));} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {if (dataInputStream != null) {try {dataInputStream.close();} catch (IOException e) {e.printStackTrace();}}}}public void testDataOutputStream() {DataOutputStream dataOutputStream = null;try {byte []by = new byte[2048];ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream(2048);dataOutputStream = new DataOutputStream(byteArrayInputStream);dataOutputStream.writeInt('5');dataOutputStream.writeChars("hahaha");dataOutputStream.writeInt(6);byte[] bs = byteArrayInputStream.toByteArray();for (byte b : bs) {System.out.print (b+" ");}//存入非字符型數(shù)據(jù)亂碼,還不知為何//0 0 0 53 0 104 0 97 0 104 0 97 0 104 0 97 0 0 0 6 } catch (Exception e) {}finally {try {if (dataOutputStream != null) {dataOutputStream.close();}} catch (IOException e) {e.printStackTrace();}}}}打印流,可用于將對象的格式表示打印到文本輸出流,相關(guān)的類PrintWriter,使用如下:
public void testPrintWriter() {PrintWriter printWriter = null;BufferedReader bufferedReader = null;try {bufferedReader = new BufferedReader(new FileReader("E:\\test\\input.txt"));printWriter = new PrintWriter(new File("E:\\test\\printwriter.txt"));String s = null;while((s = bufferedReader.readLine()) != null) {printWriter.write(s);}} catch (Exception e) {}finally {if (printWriter != null) {printWriter.close();}if (bufferedReader!=null) {try {bufferedReader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}標(biāo)準(zhǔn)輸入輸出流,相關(guān)操作如下:
/*** 標(biāo)準(zhǔn)輸入流使用*/private void testStandInputStream() {//標(biāo)準(zhǔn)輸入流,可用于鍵盤錄入Scanner scanner = new Scanner(System.in);System.out.println("=======課題選擇========");System.out.println("\t計(jì)算機(jī)原理:001");System.out.println("\t信息系統(tǒng)論:002");System.out.println("\t計(jì)算機(jī)網(wǎng)絡(luò):003");System.out.println("\t數(shù)據(jù)庫原理:004");System.out.println("====================");System.out.println("請輸入要選擇的課題代號,按回車鍵確定");String nextLine = scanner.nextLine();if ("001".equals(nextLine)) {System.out.println("選擇的課程是:計(jì)算機(jī)原理");}else if ("002".equals(nextLine)) {System.out.println("選擇的課程是:信息系統(tǒng)論");}else if ("003".equals(nextLine)) {System.out.println("選擇的課程是:計(jì)算機(jī)網(wǎng)絡(luò)");}else if ("004".equals(nextLine)) {System.out.println("選擇的課程是:數(shù)據(jù)庫原理");}System.out.println("系統(tǒng)關(guān)閉!");System.exit(-1);}//標(biāo)準(zhǔn)輸出流 private void testStandOutputStream() {PrintStream out = System.out;out.println("打印信息到控制臺");}對象輸入輸出流,也叫做序列化流,可用于對象的序列化和反序列化,就是可以實(shí)現(xiàn)將對象寫入文本、從文本中讀取信息到對象中(這里是讀取數(shù)據(jù)到對象),常用于網(wǎng)絡(luò)信息傳輸和遠(yuǎn)程方法調(diào)用,實(shí)例如下:
/*** 測試對象輸入輸出流也叫做序列化流* 通過對象輸出流可以進(jìn)行序列化,將對象寫入到文本,通過對象輸入流可以實(shí)現(xiàn)反序列化,從文本中讀取對象*/ public class ObjectStreamTest {public static void main(String[] args) {ObjectStreamTest test = new ObjectStreamTest();test.testObjectOutputStream();test.testObjectInputStream();}/*** 通過對象輸出流可以進(jìn)行序列化,將對象寫入到文本*/public void testObjectOutputStream() {ObjectOutputStream objectOutputStream = null;try {objectOutputStream = new ObjectOutputStream(new FileOutputStream("E:\\test\\user.txt"));User user = new User("小王", 13);objectOutputStream.writeObject(user);objectOutputStream.flush();objectOutputStream.close();} catch (Exception e) {e.printStackTrace();}}/*** 通過對象輸入流可以實(shí)現(xiàn)反序列化,從文本中讀取對象*/public void testObjectInputStream() {ObjectInputStream objectInputStream = null;try {objectInputStream = new ObjectInputStream(new FileInputStream("E:\\test\\user.txt"));Object readObject = objectInputStream.readObject();System.out.println(readObject);//User [name=小王, age=0]objectInputStream.close();} catch (Exception e) {e.printStackTrace();}}}/*** 對象一定要實(shí)現(xiàn)序列化接口*/ class User implements Serializable{private static final long serialVersionUID = -1796903937206347177L;private String name;private transient int age;//可以防止成員被序列化public User(String name,int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "User [name=" + name + ", age=" + age + "]";}}介紹最后一個,合并流,可用于將多個輸入流中數(shù)據(jù)合并到一個流中,使用如下:
public class AddStreamTest {/*** 將兩個流中的數(shù)據(jù)同時寫到一個文件中*/public static void main(String[] args) throws IOException {FileOutputStream fileOutputStream = new FileOutputStream("E:\\test\\input3.txt");FileInputStream fileInputStream1 = new FileInputStream("E:\\test\\input.txt");FileInputStream fileInputStream2 = new FileInputStream("E:\\test\\input2.txt");SequenceInputStream sequenceInputStream = new SequenceInputStream(fileInputStream1,fileInputStream2);byte []b = new byte[1024];int len = 0;while((len = sequenceInputStream.read(b, 0, b.length)) != -1) {fileOutputStream.write(b, 0, len);}fileOutputStream.close();sequenceInputStream.close();fileInputStream1.close();fileInputStream2.close();} }還有一些其他的流,在這里就不做介紹了。
總結(jié)
以上是生活随笔為你收集整理的12、java中的I/O流(2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 11、java中的I/O流(1)
- 下一篇: 13、字符集和字符编码