Writer使用示例
package com.io.reader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;
// Writer使用示例
public class WriterTest {
@Test
public void test01() throws IOException {
// 1. 创建需要写出的文件对象
File file = new File("b.txt");
// 2. 创建Writer输入流
FileWriter fw = new FileWriter(file);
// 3. 写出数据
fw.write("大家好!");
// 4. 关闭流
fw.close();
}
}
Reader的使用
package com.io.reader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.junit.Test;
// Reader的使用
public class ReaderTest {
/**
* 使用read(char[] buf)方法来读取数据
*/
@Test
public void test05() throws IOException {
FileReader fr = new FileReader(new File("a.txt"));
char[] cbuf = new char[4];
int len;
while ((len = fr.read(cbuf)) != -1) {
String str = new String(cbuf, 0, len);
System.out.print(str);
}
fr.close();
}
@Test
public void test04() throws IOException {
FileReader fr = new FileReader(new File("a.txt"));
char[] cbuf = new char[4];
int len;
while((len = fr.read(cbuf)) != -1) {
//for(int i=0; i<cbuf.length; i++) {
for(int i=0; i<len; i++) {
System.out.print(cbuf[i]);
}
}
fr.close();
}
/**
* 使用read()方法来每次读取一个字符,如果读到文件末尾,则返回-1
* @throws IOException
*/
@Test
public void test03() throws IOException {
File file = new File("a.txt");
FileReader fr = new FileReader(file);
int len;
while((len = fr.read()) != -1) {
System.out.print((char)len);
}
fr.close();
}
@Test
public void test02() {
// 2.创建节点流(输入流、字符流)
FileReader fr = null;
try {
// 1.把需要读取的文件封装到File对象中
File file = new File("a.txt");
fr = new FileReader(file);
// 3.循环读取,直到读到文件末尾
int n = fr.read();
while (n != -1) {
System.out.print((char) n);
n = fr.read();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 4.关闭流
try {
if (fr != null) fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 使用read()方法来每次读取一个字符,如果读到文件末尾,则返回-1
* @throws IOException
*/
@Test
public void test01() throws IOException {
// 1.把需要读取的文件封装到File对象中
File file = new File("a.txt");
// 2.创建节点流(输入流、字符流)
FileReader fr = new FileReader(file);
// 3.循环读取,直到读到文件末尾
int n = fr.read();
while(n != -1) {
System.out.print((char)n);
n = fr.read();
}
// 4.关闭流
fr.close();
/*int n = fr.read();
System.out.print((char)n);
n = fr.read();
System.out.print((char)n);
n = fr.read();
System.out.print((char)n);
n = fr.read();
System.out.print((char)n);
n = fr.read();
System.out.print((char)n);
n = fr.read();
System.out.print(n);*/
}
}
OutputStream字节输出流的使用
package com.io.outputstream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.junit.Test;
// OutputStream字节输出流的使用
public class OutputStreamTest {
@Test
public void test01() throws IOException {
OutputStream os = new FileOutputStream(new File("c.txt"));
os.write("hello".getBytes());
os.write("今天是周一。".getBytes());
os.close();
}
}
InputStream 使用
package com.io.inputstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Test;
// InputStream 使用
public class InputStreamTest {
@Test
public void tes03() throws IOException {
InputStream is = new FileInputStream(new File("a.txt"));
byte[] buf = new byte[2];
int len;
while((len = is.read(buf)) != -1) {
System.out.print(new String(buf, 0, len));
}
is.close();
}
@Test
public void test02() throws IOException {
InputStream is = new FileInputStream(new File("a.txt"));
int len;
while((len = is.read()) != -1) {
System.out.print((char)len);
}
is.close();
}
@Test
public void test01() throws IOException {
File file = new File("a.txt");
InputStream is = new FileInputStream(file);
int len = is.read();
while(len != -1) {
System.out.print((char)len);
len = is.read();
}
is.close();
}
}