您的位置:首页 > 编程语言 > Java开发

考研后的Java温习之I/O

2016-01-17 20:41 393 查看

感想(feihua)写在前面:

现在距离2016年考研结束已经20天了,在准备考研的过程中2105飞逝而过恍如昨天,直到最后一刻才算是把 所有的知识串了起来,这种考试,最后总是不如意的居多,结果固然重要,最重要的是面对事情的态度和方 法,这一点我想只要经历过高考的人都会点头同意,总之,希望是个好的结局。本来在考前就想考后的第一篇 博客会要怎么样的长篇大论会要如何的意气风发会要如何的如何,20天之后的现在,已是人去楼空客走茶凉的 景象,当时多么渴望键盘的双手现在伏在键盘上却打不出多少字来了。音乐库里已经一年没有更新了,曾经不听歌就睡不着的习惯在学习一天之后回宿舍倒头就睡下变成了回忆。还好计划还在,首先是要把基础回顾一下然后把之前会的不会的都弄会,把想学的尽量学到,当然除了编程之外的很多事也自有安排。一段时光已经

过去。

这几天其实进程很慢,因为专业内部还有考试等等,先是将servlet过了一遍然后学习了jsp这些java ee的部分基础。正准备把代码敲一遍然后学习spring,毕竟seam的应用范围不广,spring既然这么广泛 ,确实是有道理的。在这个过程中将一些容易忘的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();

}
}


some points of the java IO is located in the annotation. nonsense is unseemliness to this situation.

other points of java will be released soon. I really hope that all things don’t don’t bother me don’t detract my concenteration.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java