首页 技术 正文
技术 2022年11月8日
0 收藏 482 点赞 1,920 浏览 18242 个字

 

21.01  转换流出现的原因及格式

由于字节流操作中文不是特别方便,所以,java就提供了转换流。

字符流 = 字节流 + 编码表

21.02  编码表概述和常见编码表

编码表:计算机只能识别二进制数据,早期又来是电信号,为了方便应用计算机,让它可以识别各个国家的文字,就将各个国家的文字用数字来表示,并一一对应,形成一张表,就是编码表。

简单的说编码表就是由字符及其对应的数值组成的一张表。

 

常见的编码表:

ASCII:美国标准信息交换码,用1个字节的7位可以表示

ISO8859-1:拉丁码表,欧洲码表,用1个字节的8位表示

GBK2312:中国的中文编码表

GBK:中国的中文编码表升级,融合了更多的中文文字符号

GB18030:GBK的取代版本

BIG-5码 :通行于台湾、香港地区的一个繁体字编码方案,俗称“大五码”

Unicode:国际标准码,融合了多种文字,所有的文字都用2个字节表示,Java中使用的就是Unicode码表

UTF-8:最多用3个字节来表示一个字符(能用一个字节表示的就用一个字节,一个表示不了就用两个,最多用三个字节)

21.03  String类中的编码和解码问题

1.public String(byte[] bytes,String charsetName)throws UnsupportedEncodingException

通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。

2.public byte[] getBytes(String charsetName)throws UnsupportedEncodingException

使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

 

字符串→字节数组    编码:把看得懂的变成看不懂的

字节数组→字符串    解码:把看不懂的变成看得懂的

 

例:

 public class Practice
{
public static void main(String[] args) throws UnsupportedEncodingException
{
String s = "你好";
//编码,使用GBK码表将此字符串编码为 byte序列
byte[] bys = s.getBytes("GBK");
System.out.println(Arrays.toString(bys));//[-60, -29, -70, -61] //解码,使用GBK码表解码指定的byte数组
String ss = new String(bys,"GBK");
System.out.println(ss);//你好
}
}

Windows平台默认编码为GBK

21.04  转换流OutputStreamWriter的使用

OutputStreamWriter 字符输出流

1.public OutputStreamWriter(OutputStream out)

创建使用默认字符编码的 OutputStreamWriter。

2.public OutputStreamWriter(OutputStream out,String charsetName)throws UnsupportedEncodingException

创建使用指定字符集的 OutputStreamWriter。

例:

//默认编码GBK
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStrea("D:\\aa.txt"));
osw.write("中国");
osw.close();

21.05  转换流InputStreamReader的使用

InputStreamReader 字符输入流

public InputStreamReader(InputStream in)

创建一个使用默认字符集的 InputStreamReader。

public InputStreamReader(InputStream in,String charsetName) throws UnsupportedEncodingException

创建使用指定字符集的 InputStreamReader。

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 创建对象,默认编码GBK
// InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"));
//指定编码GBK
// InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"), "GBK");
//指定编码UTF-8
InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"), "UTF-8"); // 读取数据
// 一次读取一个字符
int ch = 0;
while ((ch = isr.read()) != -1)
{
System.out.print((char) ch);
} // 释放资源
isr.close();
}
}

21.06  字符流的5种写数据的方式

1.public void write(int c)throws IOException

写入单个字符。要写入的字符包含在给定整数值的 16 个低位中,16 高位被忽略。

2.public void write(char[] cbuf)throws IOException

写入字符数组。

3.public abstract void write(char[] cbuf,int off,int len)throws IOException

写入字符数组的某一部分。

4.public void write(String str)throws IOException

写入字符串。

5.public void write(String str,int off,int len)throws IOException

写入字符串的某一部分。

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 创建对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\osw2.txt")); // 写数据
// 写一个字符
// osw.write('a');
// osw.write(97);//osw2.txt文件中的内容为aa // 写一个字符数组
// char[] chs = {'a','b','c','d','e'};
// osw.write(chs);//osw2.txt文件中的内容abcde // 写一个字符数组的一部分
// osw.write(chs,1,3);//osw2.txt文件中的内容bcd // 写一个字符串
// osw.write("helloworld");//osw2.txt文件中的内容helloworld // public void write(String str,int off,int len):写一个字符串的一部分
osw.write("helloworld", 2, 3);//osw2.txt文件中的内容llo // 刷新缓冲区,可以继续写数据
osw.flush(); // 释放资源,关闭此流,但要先刷新它
osw.close();
}
}

面试题:close()和flush()的区别

A:close()关闭流对象,但是先刷新一次缓冲区。关闭之后,流对象不可以继续再使用了。

B:flush()仅仅刷新缓冲区,刷新之后,流对象还可以继续使用。

21.07  字符流的2种读数据的方式

1.public int read()throws IOException

读取单个字符。在字符可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。

2.public int read(char[] cbuf)throws IOException

将字符读入数组。在某个输入可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
InputStreamReader isr1 = new InputStreamReader(new FileInputStream("D:\\Demo.java"));
//一次读取一个字符
int ch = 0;
while((ch = isr1.read()) != -1)
{
System.out.print((char)ch);
} InputStreamReader isr2 = new InputStreamReader(new FileInputStream("D:\\Demo.java"));
//一次读取一个字符数组
char[] chs = new char[1024];
int len = 0;
while((len = isr2.read(chs)) != -1)
{
System.out.println(new String(chs,0,len));
} isr1.close();
isr2.close();
}
}

21.08  字符流复制文本文件案例(转换流一次读取一个字符)

数据源:a.txt — 读取数据 — 字符转换流 — InputStreamReader

目的地:b.txt — 写出数据 — 字符转换流 — OutputStreamWriter

 public class Practice
{
public static void main(String[] args) throws IOException
{
InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\Demo.java"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\bb.txt"));
//一次读取一个字符
int ch = 0;
while((ch = isr.read()) != -1)
{
osw.write(ch);
} isr.close();
osw.close();
}
}

21.09  字符流复制文本文件案例(使用便捷类)

转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,所以,为了简化我们的书写,转换流提供了对应的子类。

FileWriter以及FileReader

OutputStreamWriter = FileOutputStream + 编码表(GBK)

FileWriter = FileOutputStream + 编码表(GBK)

 

InputStreamReader = FileInputStream + 编码表(GBK)

FileReader = FileInputStream + 编码表(GBK)

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 封装数据源
FileReader fr = new FileReader("D:\\a.txt");
// 封装目的地
FileWriter fw = new FileWriter("D:\\b.txt"); // 一次一个字符
// int ch = 0;
// while ((ch = fr.read()) != -1)
//{
// fw.write(ch);
//} // 一次一个字符数组
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1)
{
fw.write(chs, 0, len);
fw.flush();
} // 释放资源
fw.close();
fr.close();
}
}

21.10  字符缓冲输出流BufferedWriter的使用

将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

可以指定缓冲区的大小,或者接受默认的大小。在大多数情况下,默认值就足够大了。

构造方法:

1.public BufferedWriter(Writer out)

创建一个使用默认大小输出缓冲区的缓冲字符输出流。

2.public BufferedWriter(Writer out,int sz)

创建一个使用给定大小输出缓冲区的新缓冲字符输出流。

例:

 BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\bw.txt"));
bw.write("hello");
bw.write("world");
bw.write("java");
bw.flush();
bw.close();

21.11  字符缓冲输入流BufferedReader的使用

从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。

可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。

构造方法:

1.public BufferedReader(Reader in)

创建一个使用默认大小输入缓冲区的缓冲字符输入流。

2.public BufferedReader(Reader in,int sz)

创建一个使用指定大小输入缓冲区的缓冲字符输入流。

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("D:\\bw.txt")); // 方式1
// int ch = 0;
// while ((ch = br.read()) != -1) {
// System.out.print((char) ch);
// } // 方式2
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1)
{
System.out.print(new String(chs, 0, len));
} // 释放资源
br.close();
}
}

21.12  字符缓冲流复制文本文件案例

数据源:a.txt — 读取数据 — 字符转换流 — InputStreamReader — FileReader — BufferedReader

目的地:b.txt — 写出数据 — 字符转换流 — OutputStreamWriter — FileWriter — BufferedWriter

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 封装数据源
BufferedReader br = new BufferedReader(new FileReader("D:\\a.txt"));
// 封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\b.txt")); // 一次读写一个字符数组
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1)
{
bw.write(chs, 0, len);
bw.flush();
} // 释放资源
bw.close();
br.close();
}
}

21.13  字符缓冲流的特殊功能

字符缓冲流的特殊方法:

BufferedWriter:

public void newLine()throws IOException

写入一个行分隔符。行分隔符字符串由系统属性 line.separator 定义,并且不一定是单个新行 (‘\n’) 符。

BufferedReader:

public String readLine()throws IOException

读取一个文本行。通过下列字符之一即可认为某行已终止:换行 (‘\n’)、回车 (‘\r’) 或回车后直接跟着换行。包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
// write();
read();
}
private static void read() throws IOException
{
// 创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("D:\\bw.txt"));
String line = null;
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
//释放资源
br.close();
} private static void write() throws IOException
{
// 创建字符缓冲输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\bw.txt"));
for (int x = 0; x < 10; x++)
{
bw.write("hello" + x);
bw.newLine();
bw.flush();
}
bw.close();
}
}

21.14  IO流小结图解

JavaSE学习总结第21天_IO流3

21.15  复制文本文件的5种方式案例

 public class Practice
{
public static void main(String[] args) throws IOException
{
String srcString = "c:\\a.txt";
String destString = "d:\\b.txt";
// method1(srcString, destString);
// method2(srcString, destString);
// method3(srcString, destString);
// method4(srcString, destString);
method5(srcString, destString);
}
// 字符缓冲流一次读写一个字符串
private static void method5(String src, String dest)throws IOException
{
BufferedReader br = new BufferedReader(new FileReader(src));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest)); String line = null;
while ((line = br.readLine()) != null)
{
bw.write(line);
bw.newLine();
bw.flush();
} bw.close();
br.close();
} // 字符缓冲流一次读写一个字符数组
private static void method4(String src, String dest)throws IOException
{
BufferedReader br = new BufferedReader(new FileReader(src));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest)); char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1)
{
bw.write(chs, 0, len);
} bw.close();
br.close();
} // 字符缓冲流一次读写一个字符
private static void method3(String src, String dest)throws IOException
{
BufferedReader br = new BufferedReader(new FileReader(src));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest)); int ch = 0;
while ((ch = br.read()) != -1)
{
bw.write(ch);
} bw.close();
br.close();
} // 基本字符流一次读写一个字符数组
private static void method2(String src, String dest)throws IOException
{
FileReader fr = new FileReader(src);
FileWriter fw = new FileWriter(dest); char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1)
{
fw.write(chs, 0, len);
} fw.close();
fr.close();
} // 基本字符流一次读写一个字符
private static void method1(String src, String dest)throws IOException
{
FileReader fr = new FileReader(src);
FileWriter fw = new FileWriter(dest); int ch = 0;
while ((ch = fr.read()) != -1)
{
fw.write(ch);
} fw.close();
fr.close();
}
}

21.16  复制图片的4种方式案例

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 使用字符串作为路径
// String srcString = "c:\\a.jpg";
// String destString = "d:\\b.jpg";
// 使用File对象做为参数
File srcFile = new File("c:\\a.jpg");
File destFile = new File("d:\\b.jpg"); // method1(srcFile, destFile);
// method2(srcFile, destFile);
// method3(srcFile, destFile);
method4(srcFile, destFile);
}
// 字节缓冲流一次读写一个字节数组
private static void method4(File srcFile, File destFile) throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile)); byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1)
{
bos.write(bys, 0, len);
} bos.close();
bis.close();
} // 字节缓冲流一次读写一个字节
private static void method3(File srcFile, File destFile) throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile)); int by = 0;
while ((by = bis.read()) != -1)
{
bos.write(by);
} bos.close();
bis.close();
} // 基本字节流一次读写一个字节数组
private static void method2(File srcFile, File destFile) throws IOException
{
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile); byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1)
{
fos.write(bys, 0, len);
} fos.close();
fis.close();
} // 基本字节流一次读写一个字节
private static void method1(File srcFile, File destFile) throws IOException
{
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile); int by = 0;
while ((by = fis.read()) != -1)
{
fos.write(by);
}
fos.close();
fis.close();
}
}

21.17  把集合中的数据存储到文本文件案例

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 封装数据源(创建集合对象)
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java"); // 封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\a.txt")); // 遍历集合
for (String s : array)
{
// 写数据
bw.write(s);
bw.newLine();
bw.flush();
} // 释放资源
bw.close();
}
}

21.18  随机获取文本文件中的姓名案例

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 把文本文件中的数据存储到集合中
BufferedReader br = new BufferedReader(new FileReader("D:\\a.txt"));
ArrayList<String> array = new ArrayList<String>();
String line = null;
while ((line = br.readLine()) != null)
{
array.add(line);
}
br.close(); // 随机产生一个索引
Random r = new Random();
int index = r.nextInt(array.size()); // 根据该索引获取一个值
String name = array.get(index);
System.out.println("该幸运者是:" + name);
}
}

21.19  复制单级文件夹案例

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 封装目录
File srcFolder = new File("e:\\demo");
// 封装目的地
File destFolder = new File("e:\\test");
// 如果目的地文件夹不存在,就创建
if (!destFolder.exists())
{
destFolder.mkdir();
} // 获取该目录下的所有文本的File数组
File[] fileArray = srcFolder.listFiles(); // 遍历该File数组,得到每一个File对象
for (File file : fileArray)
{
// System.out.println(file);
// 数据源:e:\\demo\\e.mp3
// 目的地:e:\\test\\e.mp3
String name = file.getName(); // e.mp3
File newFile = new File(destFolder, name); // e:\\test\\e.mp3 copyFile(file, newFile);
}
}
private static void copyFile(File file, File newFile) throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile)); byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1)
{
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}

21.20  复制指定目录下指定后缀名的文件并修改名称案例

需求:复制指定目录下的指定文件,并修改后缀名。

指定的文件是:.java文件

指定的后缀名是:.jad

指定的目录是:jad

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 封装目录
File srcFolder = new File("e:\\java");
// 封装目的地
File destFolder = new File("e:\\jad");
// 如果目的地目录不存在,就创建
if (!destFolder.exists())
{
destFolder.mkdir();
} // 获取该目录下的java文件的File数组
File[] fileArray = srcFolder.listFiles(new FilenameFilter()
{
@Override
public boolean accept(File dir, String name)
{
return new File(dir, name).isFile() && name.endsWith(".java");
}
}); // 遍历该File数组,得到每一个File对象
for (File file : fileArray)
{
// System.out.println(file);
// 数据源:e:\java\DataTypeDemo.java
// 目的地:e:\\jad\DataTypeDemo.java
String name = file.getName();
File newFile = new File(destFolder, name);
copyFile(file, newFile);
} // 在目的地目录下改名
File[] destFileArray = destFolder.listFiles();
for (File destFile : destFileArray)
{
// System.out.println(destFile);
// e:\jad\DataTypeDemo.java
// e:\\jad\\DataTypeDemo.jad
String name =destFile.getName(); //DataTypeDemo.java
String newName = name.replace(".java", ".jad");//DataTypeDemo.jad File newFile = new File(destFolder,newName);
destFile.renameTo(newFile);
}
}
private static void copyFile(File file, File newFile) throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile)); byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1)
{
bos.write(bys, 0, len);
} bos.close();
bis.close();
}
}

21.21  复制多级文件夹案例

需求:复制多极文件夹

数据源:E:\JavaSE\day21\code\demos

目的地:E:\\

分析:

A:封装数据源File

B:封装目的地File

C:判断该File是文件夹还是文件

   a:是文件夹

      就在目的地目录下创建该文件夹

      获取该File对象下的所有文件或者文件夹File对象

      遍历得到每一个File对象

      回到C

   b:是文件就复制(字节流)

 public class Practice
{
public static void main(String[] args) throws IOException
{
// 封装数据源File
File srcFile = new File("E:\\JavaSE\\day21\\code\\demos");
// 封装目的地File
File destFile = new File("E:\\"); // 复制文件夹的功能
copyFolder(srcFile, destFile);
}
private static void copyFolder(File srcFile, File destFile)throws IOException {
// 判断该File是文件夹还是文件
if (srcFile.isDirectory())
{
// 文件夹
File newFolder = new File(destFile, srcFile.getName());
newFolder.mkdir(); // 获取该File对象下的所有文件或者文件夹File对象
File[] fileArray = srcFile.listFiles();
for (File file : fileArray)
{
copyFolder(file, newFolder);
}
}
else
{
// 文件
File newFile = new File(destFile, srcFile.getName());
copyFile(srcFile, newFile);
}
} private static void copyFile(File srcFile, File newFile) throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile)); byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1)
{
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}

21.22  键盘录入学生信息按照总分排序并写入文本文件案例

Student类同day17 17.16

 public class Practice
{
public static void main(String[] args) throws IOException
{
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>()
{
@Override
public int compare(Student s1, Student s2)
{
//按总分比较
int num1 = s2.getSum() - s1.getSum();
//总分相同按语文成绩比较
int num2 = num1==0?s1.getChinese() - s2.getChinese():num1;
//语文成绩相同按数学成绩比较
int num3 = num2==0?s1.getMath() - s2.getMath():num2;
//数学成绩相同按英语成绩比较
int num4 = num3==0?s1.getChinese() - s2.getChinese():num3;
//英语成绩相同按姓名比较
int num5 = num4==0?s1.getName().compareTo(s2.getName()):num4;
return num5;
}
});
for (int i = 1; i <= 5; i++)
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入第"+i+"位学生的姓名");
String name = sc.nextLine();
System.out.println("请输入第"+i+"位学生的语文成绩");
String chinese = sc.nextLine();
System.out.println("请输入第"+i+"位学生的数学成绩");
String math = sc.nextLine();
System.out.println("请输入第"+i+"位学生的英语成绩");
String english = sc.nextLine(); Student s = new Student(name, Integer.parseInt(chinese), Integer.parseInt(math), Integer.parseInt(english));
ts.add(s);
}
// 遍历集合,把数据写到文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\students.txt"));
bw.write("学生信息如下:");
bw.newLine();
bw.flush();
bw.write("姓名\t语文\t数学\t英语\t总分");
bw.newLine();
bw.flush();
for (Student s : ts)
{
StringBuilder sb = new StringBuilder();
sb.append(s.getName()).append("\t").append(s.getChinese())
.append("\t").append(s.getMath()).append("\t")
.append(s.getEnglish()).append("\t").append(s.getSum());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
// 释放资源
bw.close();
System.out.println("学生信息存储完毕");
}
}

21.23  自定义类模拟BufferedReader的readLine()功能案例

用Reader模拟BufferedReader的readLine()功能

readLine():一次读取一行,根据换行符判断是否结束,只返回内容,不返回换行符

 public class MyBufferedReader
{
private Reader r; public MyBufferedReader(Reader r)
{
this.r = r;
} //思考:写一个方法,返回值是一个字符串。
public String readLine() throws IOException
{
/*
* 要返回一个字符串,看看r对象的两个读取方法,一次读取一个字符或者一次读取一个字符数组
* 很容易想到字符数组比较好,但是不能确定这个数组的长度是多长
* 所以,只能选择一次读取一个字符。
* 但是呢,这种方式的时候,我们再读取下一个字符的时候,上一个字符就丢失了
* 所以,应该定义一个临时存储空间把读取过的字符给存储起来。
* 这个用谁比较和是呢?数组,集合,字符串缓冲区三个可供选择。
* 经过简单的分析,最终选择使用字符串缓冲区对象。并且使用的是StringBuilder
*/
StringBuilder sb = new StringBuilder(); // 做这个读取最麻烦的是判断结束,但是在结束之前应该是一直读取,直到-1 /*
hello
world
java 104101108108111
119111114108100
1069711897
\r 13 \n 10
*/ int ch = 0;
while ((ch = r.read()) != -1)
{ //104,101,108,108,111
if (ch == '\r')
{
continue;
} if (ch == '\n')
{
return sb.toString(); //hello
}
else
{
sb.append((char)ch); //hello
}
} // 为了防止数据丢失,判断sb的长度不能大于0
if (sb.length() > 0)
{
return sb.toString();
}
return null;
} //关闭方法
public void close() throws IOException
{
this.r.close();
}
}

21.24  LineNumberReader的使用案例

例:

 public class Practice
{
public static void main(String[] args) throws IOException
{
LineNumberReader lnr = new LineNumberReader(new FileReader("D:\\students.txt"));
String line = null;
lnr.setLineNumber(9);//设置行号
while((line = lnr.readLine()) != null)
{
//获取行号
System.out.println(lnr.getLineNumber()+":"+line);
}
}
}

运行结果:

10:学生信息如下:
11:姓名 语文 数学 英语 总分
12:gdf 45 76 54 175
13:ftg 47 45 68 160
14:dcf 25 64 57 146
15:wdsa 23 45 76 144
16:ef 56 34 16 106

21.25  自定义类模拟LineNumberReader的获取行号功能案例

 //方式1:
public class MyLineNumberReader
{
private Reader r;
private int lineNumber = 0; public MyLineNumberReader(Reader r)
{
this.r = r;
} public int getLineNumber()
{
// lineNumber++;
return lineNumber;
} public void setLineNumber(int lineNumber)
{
this.lineNumber = lineNumber;
} public String readLine() throws IOException
{
lineNumber++; StringBuilder sb = new StringBuilder(); int ch = 0;
while ((ch = r.read()) != -1)
{
if (ch == '\r')
{
continue;
} if (ch == '\n')
{
return sb.toString();
}
else
{
sb.append((char) ch);
}
} if (sb.length() > 0)
{
return sb.toString();
} return null;
} public void close() throws IOException
{
this.r.close();
}
} //方式2:继承21.29的BufferedReader类
public class MyLineNumberReader extends MyBufferedReader
{
private Reader r; private int lineNumber = 0; public MyLineNumberReader(Reader r)
{
super(r);
} public int getLineNumber()
{
return lineNumber;
} public void setLineNumber(int lineNumber)
{
this.lineNumber = lineNumber;
} @Override
public String readLine() throws IOException
{
lineNumber++;
return super.readLine();
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918