IO 练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IO 练习相关的知识,希望对你有一定的参考价值。
//写入指定字符串(字符流&字节流)至文本文件
public class Test1 {
public static void main(String[] args) {
String str = new String(
"Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE, JavaEE, JavaME)的总称。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。");
File file = new File("test5.txt");
BufferedWriter bw = null;
try {
FileWriter fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write(str);
- bw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// BufferedOutputStream bos = null;
// try {
// FileOutputStream fos = new FileOutputStream(file);
// bos = new BufferedOutputStream(fos);
// bos.write(str.getBytes());
// bos.flush();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// try {
// if (bos != null) {
// bos.close();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}
}
//复制文本文件(字符流&字节流)
public class Test2 {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader("test5.txt"));
bw = new BufferedWriter(new FileWriter("test7.txt"));
String str;
while ((str = br.readLine()) != null) {
bw.write(str);
}
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// BufferedInputStream bis = null;
// BufferedOutputStream bos = null;
// try {
// bis = new BufferedInputStream(new FileInputStream("test5.txt"));
// bos = new BufferedOutputStream(new FileOutputStream("test6.txt"));
// byte[] b = new byte[5];
// int len;
// while ((len = bis.read(b)) != -1) {
// bos.write(b, 0, len);
// }
// bos.flush();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// try {
// if (bos != null) {
// bos.close();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// try {
// if (bis != null) {
// bis.close();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}
}
//读取指定文本文件内容并打印至控制台(字符流&字节流)
public class Test3 {
public static void main(String[] args) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("test7.txt"));
String str;
while ((str = br.readLine()) != null) {
System.out.print(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 不建议像如下所示这样使用字节流
// BufferedInputStream bis = null;
// try {
// FileInputStream fis = new
// FileInputStream("C:/Users/59929/Desktop/test6.txt");
// bis = new BufferedInputStream(fis);
// byte[] b = new byte[100];//不建议的原因是编码容易乱码
// int len;
// while ((len = bis.read(b)) != -1) {
// String str = new String(b, 0, len);
// System.out.print(str);
// }
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// try {
// if (bis != null) {
// bis.close();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// System.exit(0);
}
}
以上是关于IO 练习的主要内容,如果未能解决你的问题,请参考以下文章
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段
Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段
csharp C#代码片段 - 使类成为Singleton模式。 (C#4.0+)https://heiswayi.github.io/2016/simple-singleton-pattern-us