Java-IO-文件的创建及读取(一)
2015-11-11 12:57
716 查看
这里了解一下IO的用法。IO在网络传递数据,文件的复制,上传下载都会用到,就是以流为基础进行输入输出。根据官网API Java IO包中,流的类比较多,涉及到文件,字节流,字符流,对象流,管道流。因为知识有限,对理论不是很了解,稍微了解一点用法。这里就了解一下File类。
(一)创建一个文件夹
(二)创建一个文件
(三)读取文件中的内容
(四)向文件中写入数据
上面这三种写入数据的方法都会覆盖掉原来文本文件中的数据。但是有时候会有直接添加到原数据的结尾的需求,这时候只需要
在创建流对象的时候,调用另一个构造函数。
总结:在创建File对象时,如果给的路径下文件存在,则不会覆盖,如果不存在这个创建一个没有内容的文件。在new File(String pathname)之后,磁盘中不会显示,调用file.createNewFile()或者file.mkdir()指明file是文件夹还是其他文件类型之后,磁盘才会显示。使用FileWriter,需要关流,否则写入的文件不会显示。
(一)创建一个文件夹
//File(String pathname) 根据参数创建一个新的File实例 public static File createDir(String pathName){ File file = new File(pathName); //Creates the directory named by this abstract pathname.用pathName名字创建一个目录 file.mkdir(); return file; } //File(File parent, String child) 根据parent文件夹的路径创建一个 在parent文件夹下的文件夹 public static File createDirInParent(File parent,String child) throws IOException{ File file = new File(parent,child); file.mkdir(); return file; } public static void main(String[] args) throws Exception { String pathname = "C://aaa"; File parent = FileUtil.createDir(pathname); System.out.println(parent.getAbsolutePath()); File file = FileUtil.createDirInParent(parent, "bbbb"); if(file.exists()){ System.out.println("创建成功"); }else{ System.out.println("创建失败"); } }
(二)创建一个文件
//File(String pathname) 根据参数创建一个新的File实例 public static File createFile(String pathName) throws Exception{ File file = new File(pathName); file.createNewFile(); return file; }
(三)读取文件中的内容
//①FileInputStream public static String readTxt(File file) throws Exception{ FileInputStream fis = new FileInputStream(file); byte[] data = new byte[1024]; int len = 0; String result = null; while((len=fis.read(data))!=-1){ //data里存入了多少长度的字节文件,则创建String对象就读取多长。否则就读取1024的长度 result = new String(data, 0, len); } fis.close(); return result; } //②FileReader读取文件中的内容 public static String readTxtByReader(File file) throws Exception{ FileReader reader = new FileReader(file); //char[]数组,字符型不是字节型数据 char[] data = new char[1024]; int len = 0; String result = null; while((len=reader.read(data))!=-1){ result = new String(data, 0, len); } reader.close(); return result; }
(四)向文件中写入数据
//FileOutputStream 从一个文本文件读取数据到另一个文本文件 public static void writeTxt(File destination,File original) throws Exception{ FileOutputStream fos = new FileOutputStream(destination); FileInputStream fis = new FileInputStream(original); byte[] data = new byte[1024]; int len=0; while((len=fis.read(data))!=-1){ fos.write(data, 0, len); } fis.close(); fos.close(); } //②FileWriter public static void writeTxtByWriter(File destination,File original) throws Exception{ FileWriter writer = new FileWriter(destination); FileReader reader = new FileReader(original); char[] data = new char[1024]; while(reader.read(data)!=-1){ writer.write(data); } reader.close(); writer.close(); } //用Writer中的append方法 public static void appendToTxt(File file,String msg) throws Exception{ FileWriter writer = new FileWriter(file); writer.append(msg); writer.close(); }
上面这三种写入数据的方法都会覆盖掉原来文本文件中的数据。但是有时候会有直接添加到原数据的结尾的需求,这时候只需要
在创建流对象的时候,调用另一个构造函数。
//①java.io.FileWriter.FileWriter(File file, boolean append) throws IOException //Constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. //调用该构造方法,将第二个参数设置为true就可以添加到末尾了 public static void appendToTxt(File file,String msg) throws Exception{ FileWriter writer = new FileWriter(file,true); writer.append(msg); writer.close(); } //②java.io.FileOutputStream.FileOutputStream(File file, boolean append) throws FileNotFoundException public static void writeTxt(File destination,File original) throws Exception{ FileOutputStream fos = new FileOutputStream(destination, true); FileInputStream fis = new FileInputStream(original); byte[] data = new byte[1024]; int len=0; while((len=fis.read(data))!=-1){ fos.write(data, 0, len); } fis.close(); fos.close(); }
总结:在创建File对象时,如果给的路径下文件存在,则不会覆盖,如果不存在这个创建一个没有内容的文件。在new File(String pathname)之后,磁盘中不会显示,调用file.createNewFile()或者file.mkdir()指明file是文件夹还是其他文件类型之后,磁盘才会显示。使用FileWriter,需要关流,否则写入的文件不会显示。
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统