文件相关

  1. package com.bjsxt.io.file;
  2.  
  3. import java.io.File;
  4.  
  5. /**
  6. * 两个常量
  7. * 1、路径分隔符 ;
  8. * 2、名称分隔符 /(windows) /(linux 等)
  9. *
  10. *
  11. * @author Administrator
  12. *
  13. */
  14. public class Demo01 {
  15.  
  16. /**
  17. * @param args
  18. */
  19. public static void main(String[] args) {
  20. System.out.println(File.pathSeparator);
  21. System.out.println(File.separator);
  22. //路径表示形式
  23. String path ="E:\\xp\\test\\2.jpg";
  24. path="E:"+File.separator+"xp"+File.separator+"test"+File.separator+"2.jpg";
  25. //推荐方式
  26. path="E:/xp/test/2.jpg";
  27. }
  28.  
  29. }
  1. package com.bjsxt.io.file;
  2.  
  3. import java.io.File;
  4.  
  5. /**
  6. * 相对路径与绝对路径构造 File对象
  7. * 1、相对路径
  8. File(String parent, String child) ==>File("E:/xp/test","2.jpg")
  9. File(File parent, String child) ==> File(new File("E:/xp/test"),"2.jpg")
  10. 2、绝对路径
  11. File(String name);
  12. * @author Administrator
  13. *
  14. */
  15. public class Demo02 {
  16.  
  17. /**
  18. * @param args
  19. */
  20. public static void main(String[] args) {
  21. String parentPath ="E:/xp/test";
  22. String name ="2.jpg";
  23. //相对路径
  24. File src =new File(parentPath,name);
  25. src =new File(new File(parentPath),name);
  26. //输出
  27. System.out.println(src.getName());
  28. System.out.println(src.getPath());
  29. //绝对路径
  30. src =new File("E:/xp/test/2.jpg");
  31. System.out.println(src.getName());
  32. System.out.println(src.getPath());
  33. //没有盘符: 以 user.dir构建
  34. src =new File("test.txt");
  35. src =new File(".");
  36. System.out.println(src.getName());
  37. System.out.println(src.getPath());
  38. System.out.println(src.getAbsolutePath());
  39. }
  40.  
  41. }
  1. package com.bjsxt.io.file;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import org.junit.Test;
  7.  
  8. /**
  9. * 常用方法:
  10. 1、文件名
  11. getName() 文件名、路径名
  12. getPath()路径名
  13. getAbsoluteFile() 绝对路径所对应的File对象
  14. getAbsolutePath() 绝对路径名
  15. getParent() 父目录 ,相对路径的父目录,可能为null 如. 删除本身后的结果
  16. 2、判断信息
  17. exists()
  18. canWrite()
  19. canRead()
  20. isFile()
  21. isDirectory()
  22. isAbsolute():消除平台差异,ie以盘符开头,其他以/开头
  23. 3、长度 字节数 不能读取文件夹的长度
  24. length()
  25. 4、创建、删除
  26. createNewFile() 不存在创建新文件,存在返回false
  27. delete() 删除文件
  28. static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间
  29. staticcreateTempFile(前缀3个字节长,后缀默认.temp,目录)
  30. deleteOnExit() 退出虚拟机删除,常用于删除临时文件
  31.  
  32. * @author Administrator
  33. *
  34. */
  35. public class Demo03 {
  36.  
  37. /**
  38. * @param args
  39. */
  40. public static void main(String[] args) {
  41. try {
  42. test3();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. System.out.println("文件操作失败");
  46. } catch (InterruptedException e) {
  47. // TODO Auto-generated catch block
  48. e.printStackTrace();
  49. }
  50.  
  51. }
  52. //创建删除文件
  53. public static void test3() throws IOException, InterruptedException{
  54. //createNewFile() 不存在创建新文件
  55. //String path="E:/xp/test/con"; //con系统关键字
  56. String path="E:/xp/test/200.jpg";
  57. //String path="E:/xp/test/1.jpg";
  58. File src =new File(path);
  59. if(!src.exists()){
  60. boolean flag =src.createNewFile();
  61. System.out.println(flag?"成功":"失败");
  62. }
  63.  
  64. //删除文件
  65. boolean flag =src.delete();
  66. System.out.println(flag?"成功":"失败");
  67.  
  68. //static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间
  69. //static createTempFile(前缀3个字节长,后缀默认.temp,目录)
  70. File temp= File.createTempFile("tes", ".temp",new File("e:/xp/test"));
  71. Thread.sleep(10000);
  72. temp.deleteOnExit(); //退出即删除
  73.  
  74. }
  75.  
  76. //2、判断信息
  77. //3、长度 length()
  78. @Test
  79. public void test2(){
  80. //String path ="2.txt";
  81. String path="E:/xp/test/200.jpg";
  82. //String path="E:/xp/test/200.jpg";
  83. File src =new File(path);
  84. //是否存在
  85. System.out.println("文件是否存在:"+src.exists());
  86. //是否可读 写 canWrite() canRead()
  87. System.out.println("文件是否可写"+src.canWrite());
  88. System.out.println("============");
  89. //isFile()
  90. //isDirectory()
  91. if(src.isFile()){
  92. System.out.println("文件");
  93. }else if(src.isDirectory()){
  94. System.out.println("文件夹");
  95. }else{
  96. System.out.println("文件不存在");
  97. }
  98.  
  99. System.out.println("是否为绝对路径"+src.isAbsolute());
  100. System.out.println("长度为:"+src.length());
  101.  
  102. }
  103. //1、名称
  104. @Test
  105. public void test1(){
  106. //File src =new File("E:/xp/test/2.jpg");
  107. //建立联系
  108. File src =new File("2.txt");
  109. System.out.println(src.getName()); //返回名称
  110. System.out.println(src.getPath()); //如果是绝对路径,返回完整路径,否则相对路径
  111. System.out.println(src.getAbsolutePath());//返回绝对路径
  112. System.out.println(src.getParent());//返回上一级目录,如果是相对,返回null
  113. }
  114.  
  115. }
  1. package com.bjsxt.io.file;
  2.  
  3. import java.io.File;
  4. import java.io.FilenameFilter;
  5.  
  6. /**
  7. * 5、操作目录
  8. mkdir() 创建目录,必须确保 父目录存在,如果不存在,创建失败
  9. mkdirs() 创建目录,如果父目录链不存在一同创建
  10. list() 文件|目录 名字符串形式
  11. listFiles()
  12. static listRoots() 根路径
  13. * @author Administrator
  14. *
  15. */
  16. public class Demo04 {
  17.  
  18. /**
  19. * @param args
  20. */
  21. public static void main(String[] args) {
  22. String path ="E:/xp/test/";
  23. File src =new File(path); //文件夹
  24. if(src.isDirectory()){ //存在并且为目录
  25. System.out.println("======子目录|文件名===");
  26. String[] subNames =src.list();
  27.  
  28. for(String temp:subNames){
  29. System.out.println(temp);
  30. }
  31. System.out.println("=====子目录|文件File对象====");
  32. File[] subFiles =src.listFiles();
  33. for(File temp:subFiles){
  34. System.out.println(temp.getAbsolutePath());
  35. }
  36. System.out.println("=====子文件.java对象====");
  37. //命令设计模式
  38. subFiles =src.listFiles(new FilenameFilter(){
  39.  
  40. @Override
  41. /**
  42. * dir 代表src
  43. */
  44. public boolean accept(File dir, String name) {
  45. //System.out.println(dir.getAbsolutePath());
  46. return new File(dir,name).isFile()&&name.endsWith(".java");
  47. }
  48.  
  49. });
  50. for(File temp:subFiles){
  51. System.out.println(temp.getAbsolutePath());
  52. }
  53.  
  54. }
  55. }
  56. public static void test1(){
  57. String path ="E:/xp/test/parent/p/test";
  58. File src =new File(path);
  59. //src.mkdir();
  60. src.mkdirs();
  61. }
  62.  
  63. }
  1. package com.bjsxt.io.file;
  2.  
  3. import java.io.File;
  4. import java.io.FilenameFilter;
  5.  
  6. /**
  7. * 5、操作目录
  8. mkdir() 创建目录,必须确保 父目录存在,如果不存在,创建失败
  9. mkdirs() 创建目录,如果父目录链不存在一同创建
  10. list() 文件|目录 名字符串形式
  11. listFiles()
  12. static listRoots() 根路径
  13. * @author Administrator
  14. *
  15. */
  16. public class Demo04 {
  17.  
  18. /**
  19. * @param args
  20. */
  21. public static void main(String[] args) {
  22. String path ="E:/xp/test/";
  23. File src =new File(path); //文件夹
  24. if(src.isDirectory()){ //存在并且为目录
  25. System.out.println("======子目录|文件名===");
  26. String[] subNames =src.list();
  27.  
  28. for(String temp:subNames){
  29. System.out.println(temp);
  30. }
  31. System.out.println("=====子目录|文件File对象====");
  32. File[] subFiles =src.listFiles();
  33. for(File temp:subFiles){
  34. System.out.println(temp.getAbsolutePath());
  35. }
  36. System.out.println("=====子文件.java对象====");
  37. //命令设计模式
  38. subFiles =src.listFiles(new FilenameFilter(){
  39.  
  40. @Override
  41. /**
  42. * dir 代表src
  43. */
  44. public boolean accept(File dir, String name) {
  45. //System.out.println(dir.getAbsolutePath());
  46. return new File(dir,name).isFile()&&name.endsWith(".java");
  47. }
  48.  
  49. });
  50. for(File temp:subFiles){
  51. System.out.println(temp.getAbsolutePath());
  52. }
  53.  
  54. }
  55. }
  56. public static void test1(){
  57. String path ="E:/xp/test/parent/p/test";
  58. File src =new File(path);
  59. //src.mkdir();
  60. src.mkdirs();
  61. }
  62.  
  63. }
  1. package com.bjsxt.io.file;
  2.  
  3. import java.io.File;
  4. import java.util.Arrays;
  5.  
  6. /**
  7. * 输出子孙级目录|文件的名称(绝对路径)
  8. * 1、listFiles()
  9. * 2、递归
  10. *
  11. * static listRoots() 根路径
  12. * @author Administrator
  13. *
  14. */
  15. public class Demo05 {
  16.  
  17. /**
  18. * @param args
  19. */
  20. public static void main(String[] args) {
  21. String path ="E:/xp/test";
  22. File parent =new File(path);
  23. //printName(parent);
  24.  
  25. File[] roots =File.listRoots();
  26. System.out.println(Arrays.toString(roots));
  27. for(File temp:roots){
  28. //printName(temp);
  29.  
  30. }
  31. }
  32. /**
  33. * 输出路径
  34. */
  35. public static void printName(File src){
  36. if(null==src || !src.exists()){
  37. return ;
  38. }
  39. System.out.println(src.getAbsolutePath());
  40. if(src.isDirectory()){ //文件夹
  41. for(File sub:src.listFiles()){
  42. printName(sub);
  43. }
  44.  
  45. }
  46. }
  47.  
  48. }

IO流:

  1. package com.bjsxt.io.byteIO;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8.  
  9. /**
  10. * 文件的读取
  11. * 1、建立联系 File对象
  12. 2、选择流 文件输入流 InputStream FileInputStream
  13. 3、操作 : byte[] car =new byte[1024]; +read+读取大小
  14. 输出
  15. 4、释放资源 :关闭
  16. * @author Administrator
  17. *
  18. */
  19. public class Demo01 {
  20.  
  21. /**
  22. * @param args
  23. */
  24. public static void main(String[] args) {
  25. //1、建立联系 File对象
  26. File src =new File("e:/xp/test/a.txt");
  27. //2、选择流
  28. InputStream is =null; //提升作用域
  29. try {
  30. is =new FileInputStream(src);
  31. //3、操作 不断读取 缓冲数组
  32. byte[] car =new byte[1024];
  33. int len =0; //接收 实际读取大小
  34. //循环读取
  35. StringBuilder sb =new StringBuilder();
  36. while(-1!=(len=is.read(car))){
  37. //输出 字节数组转成字符串
  38. String info =new String(car,0,len);
  39. sb.append(info);
  40. }
  41. System.out.println(sb.toString());
  42.  
  43. } catch (FileNotFoundException e) {
  44. e.printStackTrace();
  45. System.out.println("文件不存在");
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. System.out.println("读取文件失败");
  49. }finally{
  50. try {
  51. //4、释放资源
  52. if (null != is) {
  53. is.close();
  54. }
  55. } catch (Exception e2) {
  56. System.out.println("关闭文件输入流失败");
  57. }
  58. }
  59. }
  60.  
  61. }
  1. package com.bjsxt.io.byteIO;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.OutputStream;
  8.  
  9. /**
  10. * 写出文件
  11. 1、建立联系 File对象 目的地
  12. 2、选择流 文件输出流 OutputStream FileOutputStream
  13. 3、操作 : write() +flush
  14. 4、释放资源 :关闭
  15. * @author Administrator
  16. *
  17. */
  18. public class Demo02 {
  19.  
  20. /**
  21. * @param args
  22. */
  23. public static void main(String[] args) {
  24. //1、建立联系 File对象 目的地
  25. File dest =new File("e:/xp/test/test.txt");
  26. //2、选择流 文件输出流 OutputStream FileOutputStream
  27. OutputStream os =null;
  28. //以追加形式 写出文件 必须为true 否则为覆盖
  29. try {
  30. os =new FileOutputStream(dest,true);
  31. //3、操作
  32. String str="bjsxt is very good \r\n";
  33. //字符串转字节数组
  34. byte[] data =str.getBytes();
  35. os.write(data,0,data.length);
  36.  
  37. os.flush(); //强制刷新出去
  38. } catch (FileNotFoundException e) {
  39. e.printStackTrace();
  40. System.out.println("文件未找到");
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. System.out.println("文件写出失败");
  44. }finally{
  45. //4、释放资源 :关闭
  46. try {
  47. if (null != os) {
  48. os.close();
  49. }
  50. } catch (Exception e2) {
  51. System.out.println("关闭输出流失败");
  52. }
  53. }
  54. }
  55.  
  56. }
  1. package com.bjsxt.io.byteIO;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10.  
  11. /**
  12. 1、建立联系 File对象 源头 目的地
  13. 2、选择流
  14. 文件输入流 InputStream FileInputStream
  15. 文件输出流 OutputStream FileOutputStream
  16. 3、操作 : 拷贝
  17. byte[] flush =new byte[1024];
  18. int len =0;
  19. while(-1!=(len=输入流.read(flush))){
  20. 输出流.write(flush,0,len)
  21. }
  22. 输出流.flush
  23. 4、释放资源 :关闭 两个流
  24.  
  25. * @author Administrator
  26. *
  27. */
  28. public class CopyFileDemo {
  29.  
  30. /**
  31. * @param args
  32. * @throws FileNotFoundException
  33. */
  34. public static void main(String[] args) {
  35. String src ="E:/xp/test";
  36. String dest="e:/xp/test/4.jpg";
  37. try {
  38. copyFile(src,dest);
  39. } catch (FileNotFoundException e) {
  40. e.printStackTrace();
  41. System.out.println("文件不存在");
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. System.out.println("拷贝文件失败|关闭流失败");
  45. }
  46. }
  47. /**
  48. * 文件的拷贝
  49. * @param 源文件路径
  50. * @param 目录文件路径
  51. * @throws FileNotFoundException,IOException
  52. * @return
  53. */
  54. public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
  55. //1、建立联系 源(存在且为文件) +目的地(文件可以不存在)
  56. File src =new File(srcPath);
  57. File dest =new File(destPath);
  58. if(! src.isFile()){ //不是文件或者为null
  59. System.out.println("只能拷贝文件");
  60. throw new IOException("只能拷贝文件");
  61. }
  62. //2、选择流
  63. InputStream is =new FileInputStream(src);
  64. OutputStream os =new FileOutputStream(dest);
  65. //3、文件拷贝 循环+读取+写出
  66. byte[] flush =new byte[1024];
  67. int len =0;
  68. //读取
  69. while(-1!=(len=is.read(flush))){
  70. //写出
  71. os.write(flush, 0, len);
  72. }
  73. os.flush(); //强制刷出
  74.  
  75. //关闭流
  76. os.close();
  77. is.close();
  78. }
  79. }
  1. package com.bjsxt.io.byteIO;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.OutputStream;
  12. /**
  13. * 文件操作
  14. * 1、文件拷贝
  15. * 2、文件夹拷贝 拒绝自己拷贝给自己
  16. * @author Administrator
  17. *
  18. */
  19. public class FileUtil {
  20. /**
  21. * 拷贝文件夹
  22. * @param src 源路径
  23. * @param dest 目标路径
  24. * @throws IOException
  25. * @throws FileNotFoundException
  26. */
  27. public static void copyDir(String srcPath,String destPath) throws FileNotFoundException, IOException{
  28. //拒绝自己拷贝给自己
  29. if(srcPath.equals(destPath)){
  30. return ;
  31. }
  32. File src=new File(srcPath);
  33. File dest =new File(destPath);
  34. copyDir(src,dest);
  35. }
  36.  
  37. /**
  38. * 拷贝文件夹
  39. * @param src 源File对象
  40. * @param dest 目标File对象
  41. * @throws IOException
  42. * @throws FileNotFoundException
  43. */
  44. public static void copyDir(File src,File dest) throws FileNotFoundException, IOException{
  45. if(src.isDirectory()){ //文件夹
  46. dest =new File(dest,src.getName());
  47. if(dest.getAbsolutePath().contains(src.getAbsolutePath())){
  48. System.out.println("父目录不能拷贝到子目录中");
  49. return;
  50. }
  51. }
  52. copyDirDetail(src,dest);
  53. }
  54.  
  55. /**
  56. * 拷贝文件夹细节
  57. * @param src
  58. * @param dest
  59. */
  60. public static void copyDirDetail(File src,File dest) throws FileNotFoundException,IOException{
  61. if(src.isFile()){ //文件
  62. try {
  63. FileUtil.copyFile(src, dest);
  64. } catch (FileNotFoundException e) {
  65. //e.printStackTrace();
  66. throw e;
  67. } catch (IOException e) {
  68. //e.printStackTrace();
  69. throw e;
  70. }
  71. }else if(src.isDirectory()){ //文件夹
  72. //确保目标文件夹存在
  73. dest.mkdirs();
  74. //获取下一级目录|文件
  75. for(File sub:src.listFiles()){
  76. copyDirDetail(sub,new File(dest,sub.getName()));
  77. }
  78. }
  79. }
  80.  
  81. /**
  82. * 文件的拷贝
  83. * @param 源文件路径
  84. * @param 目录文件路径
  85. * @throws FileNotFoundException,IOException
  86. * @return
  87. */
  88. public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
  89. //1、建立联系 源(存在且为文件) +目的地(文件可以不存在)
  90. copyFile(new File(srcPath),new File(destPath));
  91. }
  92. /**
  93. * 文件的拷贝
  94. * @param 源文件File对象
  95. * @param 目录文件File对象
  96. * @throws FileNotFoundException,IOException
  97. * @return
  98. */
  99. public static void copyFile(File src,File dest) throws FileNotFoundException,IOException {
  100. if(! src.isFile()){ //不是文件或者为null
  101. System.out.println("只能拷贝文件");
  102. throw new IOException("只能拷贝文件");
  103. }
  104. //dest为已经存在的文件夹,不能建立于文件夹同名的文件
  105. if(dest.isDirectory()){
  106. System.out.println(dest.getAbsolutePath()+"不能建立于文件夹同名的文件");
  107. throw new IOException(dest.getAbsolutePath()+"不能建立于文件夹同名的文件");
  108. }
  109.  
  110. //2、选择流
  111. InputStream is =new BufferedInputStream(new FileInputStream(src));
  112. OutputStream os =new BufferedOutputStream(new FileOutputStream(dest));
  113. //3、文件拷贝 循环+读取+写出
  114. byte[] flush =new byte[1024];
  115. int len =0;
  116. //读取
  117. while(-1!=(len=is.read(flush))){
  118. //写出
  119. os.write(flush, 0, len);
  120. }
  121. os.flush(); //强制刷出
  122.  
  123. //关闭流
  124. os.close();
  125. is.close();
  126. }
  127. }
  1. package com.bjsxt.io.byteIO;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6.  
  7. /**
  8. * 文件夹的拷贝
  9. * 1、文件 赋值 copyFile
  10. * 2、文件 创建 mkdirs()
  11. * 3、递归查找子孙级
  12. *
  13. * @author Administrator
  14. *
  15. */
  16. public class CopyDir {
  17.  
  18. /**
  19. * @param args
  20. */
  21. public static void main(String[] args) {
  22. //源目录
  23. String srcPath="E:/xp/test/a";
  24. //目标目录
  25. String destPath="E:/xp/test/a/b";
  26. try {
  27. FileUtil.copyDir(srcPath,destPath);
  28. } catch (FileNotFoundException e) {
  29. e.printStackTrace();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33.  
  34. }
  35. /**
  36. * 拷贝文件夹
  37. * @param src 源路径
  38. * @param dest 目标路径
  39. */
  40. public static void copyDir(String srcPath,String destPath){
  41. File src=new File(srcPath);
  42. File dest =new File(destPath);
  43. copyDir(src,dest);
  44. }
  45.  
  46. /**
  47. * 拷贝文件夹
  48. * @param src 源File对象
  49. * @param dest 目标File对象
  50. */
  51. public static void copyDir(File src,File dest){
  52. if(src.isDirectory()){ //文件夹
  53. dest =new File(dest,src.getName());
  54. }
  55. copyDirDetail(src,dest);
  56. }
  57.  
  58. /**
  59. * 拷贝文件夹细节
  60. * @param src
  61. * @param dest
  62. */
  63. public static void copyDirDetail(File src,File dest){
  64. if(src.isFile()){ //文件
  65. try {
  66. FileUtil.copyFile(src, dest);
  67. } catch (FileNotFoundException e) {
  68. e.printStackTrace();
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }else if(src.isDirectory()){ //文件夹
  73. //确保目标文件夹存在
  74. dest.mkdirs();
  75. //获取下一级目录|文件
  76. for(File sub:src.listFiles()){
  77. copyDirDetail(sub,new File(dest,sub.getName()));
  78. }
  79. }
  80. }
  81.  
  82. }
  1. package com.bjsxt.io.charIO;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.Reader;
  8.  
  9. /**
  10. * 纯文本读取
  11. * @author Administrator
  12. *
  13. */
  14. public class Demo01 {
  15.  
  16. /**
  17. * @param args
  18. */
  19. public static void main(String[] args) {
  20. //创建源
  21. File src =new File("E:/xp/test/a.txt");
  22. //选择流
  23. Reader reader =null;
  24. try {
  25. reader =new FileReader(src);
  26. //读取操作
  27. char[] flush =new char[1024];
  28. int len =0;
  29. while(-1!=(len=reader.read(flush))){
  30. //字符数组转成 字符串
  31. String str =new String(flush,0,len);
  32. System.out.println(str);
  33. }
  34. } catch (FileNotFoundException e) {
  35. e.printStackTrace();
  36. System.out.println("源文件不存在");
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. System.out.println("文件读取失败");
  40. }finally{
  41. try {
  42. if (null != reader) {
  43. reader.close();
  44. }
  45. } catch (Exception e2) {
  46. }
  47. }
  48. }
  49.  
  50. }
  1. package com.bjsxt.io.charIO;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.Writer;
  8.  
  9. /**
  10. * 写出文件
  11. * @author Administrator
  12. *
  13. */
  14. public class Demo02 {
  15.  
  16. /**
  17. * @param args
  18. */
  19. public static void main(String[] args) {
  20. //创建源
  21. File dest =new File("e:/xp/test/char.txt");
  22. //选择流
  23. Writer wr =null;
  24. try {
  25. //追加文件,而不是覆盖文件
  26. wr =new FileWriter(dest);
  27. //写出
  28. String msg ="追加.....锄禾日当午\r\n码农真辛苦\r\n一本小破书\r\n一读一上午";
  29. wr.write(msg);
  30. wr.append("倒萨发了看电视剧 ");
  31.  
  32. wr.flush();
  33. } catch (FileNotFoundException e) {
  34. e.printStackTrace();
  35. }catch (IOException e) {
  36. e.printStackTrace();
  37. }finally{
  38. try {
  39. if (null != wr) {
  40. wr.close();
  41. }
  42. } catch (Exception e2) {
  43. }
  44. }
  45. }
  46.  
  47. }
  1. package com.bjsxt.io.charIO;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.Reader;
  9. import java.io.Writer;
  10.  
  11. /**
  12. * 纯文本拷贝
  13. * @author Administrator
  14. *
  15. */
  16. public class CopyFileDemo {
  17.  
  18. /**
  19. * @param args
  20. */
  21. public static void main(String[] args) {
  22. //创建源 仅限于 字符的纯文本
  23. File src =new File("E:/xp/test/Demo03.java");
  24. File dest =new File("e:/xp/test/char.txt");
  25. //选择流
  26. Reader reader =null;
  27. Writer wr =null;
  28. try {
  29. reader =new FileReader(src);
  30. wr =new FileWriter(dest);
  31. //读取操作
  32. char[] flush =new char[1024];
  33. int len =0;
  34. while(-1!=(len=reader.read(flush))){
  35. wr.write(flush, 0, len);
  36. }
  37. wr.flush();//强制刷出
  38. } catch (FileNotFoundException e) {
  39. e.printStackTrace();
  40. System.out.println("源文件不存在");
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. System.out.println("文件读取失败");
  44. }finally{
  45. try {
  46. if (null != wr) {
  47. wr.close();
  48. }
  49. } catch (Exception e2) {
  50. }
  51. try {
  52. if (null != reader) {
  53. reader.close();
  54. }
  55. } catch (Exception e2) {
  56. }
  57. }
  58.  
  59. }
  60.  
  61. }

IO(一)的更多相关文章

  1. VS2015编译GEOS

    下载链接:http://trac.osgeo.org/geos/ 1. 打开cmake,加载geos源码和定位geos的工程存放位置: 2.点击configure,会报错,首先设置CMAKE_INST ...

  2. 高性能IO模型浅析

    高性能IO模型浅析 服务器端编程经常需要构造高性能的IO模型,常见的IO模型有四种: (1)同步阻塞IO(Blocking IO):即传统的IO模型. (2)同步非阻塞IO(Non-blocking  ...

  3. 深究标准IO的缓存

    前言 在最近看了APUE的标准IO部分之后感觉对标准IO的缓存太模糊,没有搞明白,APUE中关于缓存的部分一笔带过,没有深究缓存的实现原理,这样一本被吹上天的书为什么不讲透彻呢?今天早上爬起来赶紧找了 ...

  4. [APUE]标准IO库(下)

    一.标准IO的效率 对比以下四个程序的用户CPU.系统CPU与时钟时间对比 程序1:系统IO 程序2:标准IO getc版本 程序3:标准IO fgets版本 结果: [注:该表截取自APUE,上表中 ...

  5. [APUE]标准IO库(上)

    一.流和FILE对象 系统IO都是针对文件描述符,当打开一个文件时,即返回一个文件描述符,然后用该文件描述符来进行下面的操作,而对于标准IO库,它们的操作则是围绕流(stream)进行的. 当打开一个 ...

  6. [.NET] 利用 async & await 进行异步 IO 操作

    利用 async & await 进行异步 IO 操作 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6082673.html  序 上次,博主 ...

  7. [原] KVM 虚拟化原理探究(6)— 块设备IO虚拟化

    KVM 虚拟化原理探究(6)- 块设备IO虚拟化 标签(空格分隔): KVM [toc] 块设备IO虚拟化简介 上一篇文章讲到了网络IO虚拟化,作为另外一个重要的虚拟化资源,块设备IO的虚拟化也是同样 ...

  8. [原] KVM 虚拟化原理探究(5)— 网络IO虚拟化

    KVM 虚拟化原理探究(5)- 网络IO虚拟化 标签(空格分隔): KVM IO 虚拟化简介 前面的文章介绍了KVM的启动过程,CPU虚拟化,内存虚拟化原理.作为一个完整的风诺依曼计算机系统,必然有输 ...

  9. Performance Monitor4:监控SQL Server的IO性能

    SQL Server的IO性能受到物理Disk的IO延迟和SQL Server内部执行的IO操作的影响.在监控Disk性能时,最主要的度量值(metric)是IO延迟,IO延迟是指从Applicati ...

  10. java.IO输入输出流:过滤流:buffer流和data流

    java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...

随机推荐

  1. 【BZOJ】【2705】【SDOI2012】Longge的问题

    欧拉函数/狄利克雷卷积/积性函数 2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1275  Solv ...

  2. Centos编译安装PHP 5.5笔记

    本篇是在 Centos 6.4 32bit 下编译安装 php 5.5.5 的笔记,接上篇 Centos编译安装Apache 2.4.6笔记.php 5.5.x 和 centos 源里面的 php 5 ...

  3. Nodejs Express 4.X 中文API 4--- Router篇

    相关阅读: Express 4.X API 翻译[一] --  Application篇 Express4.XApi 翻译[二] --  Request篇 Express4.XApi 翻译[三] -- ...

  4. iOS 面试题

    1.Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答:不可以,可以实现多个接口:category是分类,,一般情况下分类 ...

  5. Redis与Memcached的incr/decr差异对比

    目前广泛使用的分布式缓存Redis和Memcached均支持对整数型Value值的增减,对应到具体命令中就是incr和decr命令. incr/decr是原子性操作(memcached 1.2.4及以 ...

  6. SSH开发实践part2:双向1-N连接配置

    1 OK,上一篇已经介绍了项目开发的前期准备工作,具体内容可以参考:http://www.cnblogs.com/souvenir/p/3783686.html 按照开发步骤,我们现在已经可以开始进行 ...

  7. lintcode: 堆化

    堆化 给出一个整数数组,堆化操作就是把它变成一个最小堆数组. 对于堆数组A,A[0]是堆的根,并对于每个A[i],A [i * 2 + 1]是A[i]的左儿子并且A[i * 2 + 2]是A[i]的右 ...

  8. Android核心分析之二十一Android应用框架之AndroidApplication

    Android Application Android提供给开发程序员的概念空间中Application只是一个松散的表征概念,没有多少实质上的表征.在Android实际空间中看不到实际意义上的应用程 ...

  9. Java-J2SE学习笔记-字符串转化为二维数组

    1.字符串转化为二维Double数组 2.代码: package Test; public class TestDouble { public static void main(String[] ar ...

  10. SWD接口:探索&泄密&延伸

    http://bbs.21ic.com/icview-871133-1-1.html 文买了个JLINKV9,以为神器,拿到手发现根本不是,完全没必要替换V8,想自己做个另类的调试器,当然想只是想而已 ...