java流强化练习
程序员文章站
2024-03-04 21:48:24
...
相关类
- InputStream
- OutputStream.
- InputStreamReader
- OutputStreamWriter
- BufferedReader
- BufferedWriter
项目结构
练习代码
import org.junit.Test;
import java.io.*;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class JavaStream {
/**
* inputstream outputstream
*
* @throws IOException
*/
@Test
public void test0() throws IOException {
InputStream is = new FileInputStream("/Users/huanghainongye/Desktop/IdeaProjects/java-demo/1.png");
OutputStream os = new FileOutputStream("/Users/huanghainongye/Desktop/IdeaProjects/java-demo/test/2.png");
int i = is.read();
while (i > 0) {
os.write(i);
i = is.read();
}
is.close();
os.close();
}
/**
* ObjectOutputStream 将对象写入文件
* @throws IOException
*/
@Test
public void test1() throws IOException {
String filepath = "/Users/huanghainongye/Desktop/IdeaProjects/java-demo/lhw.json";
File file = new File(filepath);
if(!file.exists()) {
file.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(filepath);
ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
oos.writeBoolean(true);
oos.writeByte((byte) 65);
oos.writeChar('a');
oos.writeInt(20131015);
oos.writeFloat(3.14F);
oos.writeDouble(1.414D);
oos.writeUTF("uudd");
// 写入HashMap对象
HashMap<String, String> map = new HashMap<String, String>();
map.put("one", "red");
map.put("two", "green");
map.put("three", "blue");
oos.writeObject(map);
Bean bean = new Bean();
bean.setCode(1);
bean.setDescription("beanData");
bean.setData(map);
oos.writeObject(bean);
oos.close();
fileOutputStream.close();
}
/**
* ObjectInputStream 将文件中的对象内容写入内存
* @throws IOException
* @throws ClassNotFoundException
*/
@Test
public void test2() throws IOException, ClassNotFoundException {
String filepath = "/Users/huanghainongye/Desktop/IdeaProjects/java-demo/lhw.json";
FileInputStream fis = new FileInputStream(filepath);
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.printf("boolean:%b\n" , ois.readBoolean());
System.out.printf("byte:%d\n" , (ois.readByte()&0xff));
System.out.printf("char:%c\n" , ois.readChar());
System.out.printf("int:%d\n" , ois.readInt());
System.out.printf("float:%f\n" , ois.readFloat());
System.out.printf("double:%f\n" , ois.readDouble());
System.out.printf(ois.readUTF());
//去读 HashMap对象
HashMap map = (HashMap)ois.readObject();
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
System.out.printf("%-6s -- %s\n" , entry.getKey(), entry.getValue());
}
// // 读取Bean对象
Bean bean = (Bean) ois.readObject();
System.out.println("bean:" + bean);
ois.close();
fis.close();
}
/**
* BufferedInputStream BufferedOutputStream 字节缓冲流提升读写效率
* @throws IOException
*/
@Test
public void test3() throws IOException {
InputStream is = new FileInputStream("/Users/huanghainongye/Desktop/IdeaProjects/java-demo/1.png");
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os = new FileOutputStream("/Users/huanghainongye/Desktop/IdeaProjects/java-demo/test/1333.png");
BufferedOutputStream bos = new BufferedOutputStream(os);
byte[] bytes = new byte[1024];
int read = bis.read(bytes);
while (read > 0) {
read = bis.read(bytes);
bos.write(bytes);
}
bis.close();
is.close();
bos.close();
os.close();
}
/**
* FileReader FileWriter读取 写入文件
* @throws IOException
*/
@Test
public void test4() throws IOException {
String filePath = "/Users/huanghainongye/Desktop/IdeaProjects/java-demo/text.txt";
Reader reader = new FileReader(filePath);
Writer writer = new FileWriter("/Users/huanghainongye/Desktop/IdeaProjects/java-demo/test/text.txt");
int read = reader.read();
while (read > 0) {
writer.write(read);
read = reader.read();
}
reader.close();
writer.close();
}
/**
* BufferedReader BufferedWriter 字符流高效读取
* @throws IOException
*/
@Test
public void test5() throws IOException {
String filePath = "/Users/huanghainongye/Desktop/IdeaProjects/java-demo/text.txt";
Reader reader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(reader);
Writer writer = new FileWriter("/Users/huanghainongye/Desktop/IdeaProjects/java-demo//test/text2.txt");
BufferedWriter bufferedWriter = new BufferedWriter(writer);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
bufferedWriter.append(line);
bufferedWriter.newLine();
}
bufferedWriter.flush();
bufferedReader.close();
reader.close();
bufferedWriter.close();
writer.close();
}
/**
* InputStream 转 InputStreamReader 输出(字符流比字节流效率高)
* @throws IOException
*/
@Test
public void test6() throws IOException {
String filePath = "/Users/huanghainongye/Desktop/IdeaProjects/java-demo/text.txt";
InputStream is = new FileInputStream(filePath);
InputStreamReader inputStreamReader = new InputStreamReader(is);
String temp = "";
int read = inputStreamReader.read();
while (read > 0) {
temp+=(char) read;
read = inputStreamReader.read();
}
System.out.println(temp);
inputStreamReader.close();
is.close();
}
/**
* InputStream OutputStream. InputStreamReader OutputStreamWriter BufferedReader BufferedWriter之间的转换
* @throws IOException
*/
@Test
public void test7() throws IOException {
String filePath = "/Users/huanghainongye/Desktop/IdeaProjects/java-demo/text.txt";
InputStream is = new FileInputStream(filePath);
InputStreamReader inputStreamReader = new InputStreamReader(is);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
OutputStream os = new FileOutputStream("/Users/huanghainongye/Desktop/IdeaProjects/java-demo/testext5.txt");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(os);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
String temp = null;
while ((temp = bufferedReader.readLine()) != null) {
bufferedWriter.append(temp);
bufferedWriter.newLine();
}
bufferedWriter.flush();
bufferedReader.close();
inputStreamReader.close();
is.close();
bufferedWriter.close();
outputStreamWriter.close();
os.close();
}
}