黑马程序员 java IO BufferedReader BufferdWriter 读文件 写文件
2014-04-09 13:22
232 查看
public class I_05_IO_MyBufferedReader {
/**
* @param args
*/
static FileReader fr;
public I_05_IO_MyBufferedReader(FileReader fr) {
this.fr = fr;
}
public static String myReadLine() throws IOException {
StringBuilder sb = new StringBuilder();
int flag = -1;
while ((flag = fr.read()) != -1) {
if ((char) flag == '\r')
continue;
if ((char) flag == '\n')
return sb.toString();
if (sb.length() != 0)
sb.toString();
sb.append((char) flag);
}
return null;
}
public static void myClose() throws IOException {
if (fr != null)
fr.close();
}
public static void main(String[] args) {
FileReader fr = null;
I_05_IO_MyBufferedReader myBr = null;
try {
String str;
fr = new FileReader(I_04_IO.path + I_04_IO.fileName);
myBr = new I_05_IO_MyBufferedReader(fr);
while ((str = myBr.myReadLine()) != null) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
myBr.myClose();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @param args
*/
static FileReader fr;
public I_05_IO_MyBufferedReader(FileReader fr) {
this.fr = fr;
}
public static String myReadLine() throws IOException {
StringBuilder sb = new StringBuilder();
int flag = -1;
while ((flag = fr.read()) != -1) {
if ((char) flag == '\r')
continue;
if ((char) flag == '\n')
return sb.toString();
if (sb.length() != 0)
sb.toString();
sb.append((char) flag);
}
return null;
}
public static void myClose() throws IOException {
if (fr != null)
fr.close();
}
public static void main(String[] args) {
FileReader fr = null;
I_05_IO_MyBufferedReader myBr = null;
try {
String str;
fr = new FileReader(I_04_IO.path + I_04_IO.fileName);
myBr = new I_05_IO_MyBufferedReader(fr);
while ((str = myBr.myReadLine()) != null) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
myBr.myClose();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- 黑马程序员——文件的切割,与合并
- 黑马程序员-[C语言] 第四篇:文件操作总结
- 黑马程序员-学习日志-文件的合并
- 黑马程序员_Java打开任意格式文件
- 黑马程序员<文件操作流(css文件压缩)>
- 黑马程序员_HashCode_框架_配置文件
- 黑马程序员--用类加载器的方式管理资源和配置文件
- 黑马程序员:java文件复制问题
- 黑马程序员——java IO实现文件拷贝
- 黑马程序员--OC自学笔记---02对象存储细节、NSString简介、多文件开发
- 黑马程序员—C学习笔记—第一次写头文件
- 黑马程序员之C#学习笔记:C#读取txt文件的方法
- 黑马程序员 将dataGridView数据导出成EXECL文件
- 黑马程序员_java 配置文件RunTime的实现
- 黑马程序员--数组的反射,反射的实现框架实例,配置文件加载
- 黑马程序员——IO流复制文件
- 黑马程序员——Java基础——IO流(拷贝文件)
- 黑马程序员———C语言———【枚举、typedef、宏、static和extern、文件操作】
- 黑马程序员_第四篇:一个将文件导入数据库的例子
- 黑马程序员--C语言入门疑难点----进制,文件