import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
public class BufferedReaderTest {
private static final int LEN = 5;
public static void main(String[] args) {
testBufferedReader() ;
}
/**
* BufferedReader的API测试函数
*/
private static void testBufferedReader() {
// 创建BufferedReader字符流,内容是ArrayLetters数组
try {
File file = new File("bufferedreader.txt");
BufferedReader in =
new BufferedReader(
new FileReader(file));
// 从字符流中读取5个字符。“abcde”
for (int i=0; i<LEN; i++) {
// 若能继续读取下一个字符,则读取下一个字符
if (in.ready()) {
// 读取“字符流的下一个字符”
int tmp = in.read();
System.out.printf("%d : %c\n", i, tmp);
}
}
// 若“该字符流”不支持标记功能,则直接退出
if (!in.markSupported()) {
System.out.println("make not supported!");
return ;
}
// 标记“当前索引位置”,即标记第6个位置的元素--“f”
// 1024对应marklimit
in.mark(1024);
// 跳过22个字符。
in.skip(22);
// 读取5个字符
char[] buf = new char[LEN];
in.read(buf, 0, LEN);
System.out.printf("buf=%s\n", String.valueOf(buf));
// 读取该行剩余的数据
System.out.printf("readLine=%s\n", in.readLine());
// 重置“输入流的索引”为mark()所标记的位置,即重置到“f”处。
in.reset();
// 从“重置后的字符流”中读取5个字符到buf中。即读取“fghij”
in.read(buf, 0, LEN);
System.out.printf("buf=%s\n", String.valueOf(buf));
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
abcdefghijklmnopqrstuvwxyz 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ
0 : a 1 : b 2 : c 3 : d 4 : e buf=01234 readLine=56789 buf=fghij
import java.io.BufferedWriter;
import java.io.File;
import java.io.OutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Scanner;
public class BufferedWriterTest {
private static final int LEN = 5;
// 对应英文字母“abcdefghijklmnopqrstuvwxyz”
//private static final char[] ArrayLetters = "abcdefghijklmnopqrstuvwxyz";
private static final char[] ArrayLetters = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
public static void main(String[] args) {
testBufferedWriter() ;
}
/**
* BufferedWriter的API测试函数
*/
private static void testBufferedWriter() {
// 创建“文件输出流”对应的BufferedWriter
// 它对应缓冲区的大小是16,即缓冲区的数据>=16时,会自动将缓冲区的内容写入到输出流。
try {
File file = new File("bufferwriter.txt");
BufferedWriter out =
new BufferedWriter(
new FileWriter(file));
// 将ArrayLetters数组的前10个字符写入到输出流中
out.write(ArrayLetters, 0, 10);
// 将“换行符\n”写入到输出流中
out.write('\n');
out.flush();
//readUserInput() ;
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取用户输入
*/
private static void readUserInput() {
System.out.println("please input a text:");
Scanner reader=new Scanner(System.in);
// 等待一个输入
String str = reader.next();
System.out.printf("the input is : %s\n", str);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有