考研为什么要取消java_考研后的Java溫習之I/O

package test;

import java.io.*;

import java.util.Date;

public class LearnIo {

// InputStream-- OutputStream-- Reader-- Writer

public void read1() {

FileInputStream in = null;

try {

in = new FileInputStream("D:\\note.txt");

} catch (FileNotFoundException e) {

System.out.println("Can't find relevent files!");

System.exit(-1);

}

try {

long num = 0;

int b = 0;

while ((b = in.read()) != -1) {

System.out.print((char) b);

num++;

}

in.close();

System.out.println();

System.out.println("一共讀取了" + num + "個字符");

} catch (IOException ee) {

System.out.println("file can't read correct!");

System.exit(-1);

}

}// 這樣寫的話中文出現亂碼問題!

// +++++++++++++++++++++++++++++++++++++++++++++

public void read2() {

FileReader fr = null;

try {

fr = new FileReader("D:\\note.txt");

} catch (FileNotFoundException e) {

System.out.println("Can't find relevent files!");

System.exit(-1);

}

try {

long ln = 0;

int c = 0;

while ((c = fr.read()) != -1) {

System.out.print((char) c);

ln++;

}

fr.close();

System.out.println();

System.out.println("一共讀取了" + ln + "行");

} catch (IOException ee) {

System.out.println("file can't read correct!");

System.exit(-1);

}

}//

// 下面使用緩沖流 BufferedReader/Writer BufferedInputStream/OutputStream

public void testbufferStream1() {

try {

BufferedInputStream bis = new BufferedInputStream(

new FileInputStream("D:\\note.txt"));

int d = 0;

System.out.println(bis.read());

System.out.println(bis.read());

bis.mark(30);// 用的比較少!

for (int i = 0; i <= 10 && (d = bis.read()) != -1; i++) {

System.out.print((char) d + " ");

}

System.out.println();

bis.reset();// 用的比較少!

for (int i = 0; i <= 10 && (d = bis.read()) != -1; i++) {

System.out.print((char) d + " ");

}

bis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

public void testBufferedStream2() {

try {

BufferedReader br = new BufferedReader(new FileReader(

"/home/yourname/javacode/note.txt"));// D:\\note.txt

// BufferedWriter bw = new BufferedWriter(new FileWriter(

// "/home/zsx/javacode/note.txt"));

String es = null;

while ((es = br.readLine()) != null) {

String ess = new String(es.getBytes("utf-8"));

System.out.println(ess);

// bw.write(e);

}

// 讀取txt文件亂碼問題解決!

// notice!IO里面涉及了設計模式,BufferedReader經常使用readLine方法,Input/OutputStream

// 才有設置字符編碼參數的方法,BufferedReader br=new BufferedReader(new

// InputStreamReader(new

// FileInputStream("path+"/"+title"),"UTF-8"));

} catch (IOException ee) {

ee.printStackTrace();

}

}

// 字節數據和字符數據之間的轉換InputStreamReader and InputStream OutputStreamWriter and

// OutputStream

public void testTransform1() {

try {

OutputStreamWriter osw = new OutputStreamWriter(

new FileOutputStream("/home/yourname/javacode/note_1.txt"));

osw.write("qwertyuiop");

System.out.println(osw.getEncoding());

osw.close();

osw = new OutputStreamWriter(new FileOutputStream(

"/home/yourname/javacode/note_1.txt", true));

osw.write("qwertyuiop學徒學徒學徒");

osw.flush();

osw.close();// 流一定要關閉!關閉前一定要flush!

System.out.println("done!");

} catch (IOException ex1) {

ex1.printStackTrace();

}

}

// ctrl+shift+b 設置斷點 ,F5是跳進,F6是執行下一步,F7是跳出

public void testTransform2() {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// System.in是InputStream

String s = null;

try {

s = br.readLine();

while (null != s) {

if (s.equalsIgnoreCase("exit")) {

System.out

.println("program has exited! don't call it again");

break;

}

System.out.println(s.toUpperCase());

s = br.readLine();

}

br.close();

} catch (IOException ex2) {

ex2.printStackTrace();

}

}

// DataInputStream,DataOutputStream 提供處理與機器無關的java 原始類型數據eg.:int float ...

public void testDataStream() {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

DataOutputStream dos = new DataOutputStream(baos);

try {

dos.writeDouble(Math.random());

dos.writeBoolean(true);

DataInputStream dis = new DataInputStream(new ByteArrayInputStream(

baos.toByteArray()));

System.out.println(dis.readDouble());

System.out.println(dis.readBoolean());

dos.close();

dis.close();

} catch (IOException et) {

et.printStackTrace();

}

}

// Print流

public void testPrintStream1() {

PrintStream ps = null;

try {

FileOutputStream fos = new FileOutputStream(

"/home/yourname/javacode/testPrintStream1.txt");

ps = new PrintStream(fos);

} catch (IOException e) {

e.printStackTrace();

}

if (null != ps) {

System.out.println(ps);

}

int line = 0;

for (char c = 0; c <= 2000; c++) {

System.out.println(c + " ");

if (line++ >= 100) {

System.out.println();

line = 0;

}

}

}

public void testPrintStream2(String f, PrintStream fs) {

try {

BufferedReader br = new BufferedReader(new FileReader(f));

String s = null;

while ((s = br.readLine()) != null) {

fs.print(s);

}

br.close();

} catch (IOException exx) {

fs.println("無法讀取文件");

}

}

public void testPrintStream3() {

String s = null;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try {

FileWriter fw = new FileWriter(

"/home/yourname/javacode/testPrintStream.log", true);

PrintWriter log = new PrintWriter(fw);

while ((s = br.readLine()) != null) {

if (s.equalsIgnoreCase("exit"))

break;

System.out.println(s.toUpperCase());

log.println("-----------");

log.println(s.toUpperCase());

log.flush();

}

log.println("======" + new Date() + "=====");

log.flush();

log.close();

} catch (IOException exxx) {

exxx.printStackTrace();

}

}

public void testObjectIo() {

TestClass ts = new TestClass();

ts.tall = 8;

try {

ObjectOutputStream oos = new ObjectOutputStream(

new FileOutputStream("/home/yourname/javacode/testObjectIo.dat"));

oos.writeObject(ts);

oos.flush();

oos.close();

// ~~~~~~~~~~~~~

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(

"/home/yourname/javacode/testObjectIo.dat"));

TestClass tts;

tts = (TestClass) ois.readObject();

System.out.println(tts.tall + " " + tts.name);

} catch (IOException e8) {

System.out.println("處理出現問題!");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

// 為了便於測試方法:使用成員內部類

class TestClass {

int tall;

String name;

}

public static void main(String[] args) {

LearnIo learnIo = new LearnIo();

// learnIo.read1();

// learnIo.read2();

// learnIo.testbufferStream1();

// learnIo.testBufferedStream2();

// learnIo.testTransform1();

// learnIo.testTransform2();

// learnIo.testDataStream();

// learnIo.testPrintStream1();

// learnIo.testPrintStream2("/home/yourname/javacode/note.txt", System.out);

// learnIo.testPrintStream3();

learnIo.testObjectIo();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值