标签:code sys 互联网 puts pre size 移动电话 line cat
//写入指定字符串(字符流&字节流)至文本文件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);}}
标签:code sys 互联网 puts pre size 移动电话 line cat
原文地址:http://www.cnblogs.com/chendifan/p/6536615.html