Java学习笔记(三)——文件读写操作
来源:互联网 发布:对网络直播的利与弊 编辑:程序博客网 时间:2024/05/28 23:22
package systemManager;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;/** * * @author chencong 文件管理系统 */public class FileManager { /** * 在指定文件路径下创建文件夹或者是在项目根目录下创建文件夹 * * @param dirName * @return 返回创建文件夹的路径 */ public String createDir(String dirName) { if (dirName == null) { return null; } File file = new File(dirName); if (file.exists()) { file.delete(); } file.mkdir(); return file.getAbsolutePath(); } /** * 在指定位置创建文件 返回文件的相对路径 * * @param fileName * @return 返回文件的绝对路径 */ public String createFile(String fileName) { if (fileName == null) { return null; } File file = new File(fileName); if (file.exists()) { file.delete(); } try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } return file.getAbsolutePath(); } /** * 向指定文件中写入内容,如果该文件不存在,则在指定位置创建该文件再写入 * * @param fileName * 指定文件的路径名称 * @param value * 将要向该文件中写入的内容 * @throws Exception * 抛出异常 */ public void setSomeByFile(String fileName, String value) throws Exception { if (fileName == null || value == null) { return; } File file = new File(fileName); if (!file.exists()) { file.createNewFile(); } FileOutputStream outputStream = new FileOutputStream(file, true); byte[] bytes = value.getBytes("utf-8"); for (byte b : bytes) { outputStream.write(b); } outputStream.close(); } /** * 返回指定位置文件的内容,如果该文件不存在或者该文件为文件夹直接返回null * * @param fileName * 将要获得内容的文件路径 * @return 返回null 或者 是文件的内容 */ public String getSomeByFile(String fileName) { if (fileName == null) { return null; } File file = new File(fileName); // 如果指定路径文件不存在 直接返回空 ,传入路径为目录则直接返回空 if (!file.exists() || file.isDirectory()) { return null; } // 否则获取该文件中的内容 StringBuffer sBuffer = new StringBuffer(); try { String msg = null; // 创建字符流 FileReader fileReader = new FileReader(file); // 创建字符缓冲流 BufferedReader bufferedReader = new BufferedReader(fileReader); while ((msg = bufferedReader.readLine()) != null) { sBuffer.append(msg); } bufferedReader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } return sBuffer.toString(); } /** * 将源文件内容复制到目的地址文件当中。 * <P> * 如果源文件名为null 或者目的地址为null 则统一放回null * <p> * 如果原地址不为空且为文件则将其内容返回并copy进入目的地址 * <p> * 如果目的地址文件不存在且目的地址正确为文件名,则创建一个文件 * * @param sourceName * 源文件名 * @param destName * 目的文件名 * @return 返回源文件当中的内容 * @throws Exception */ public String copyFileByName(String sourceName, String destName) throws Exception { if (sourceName == null || destName == null) { return null; } File sourceFile = new File(sourceName); // 获得源文件当中的内容 String sourceMsg = null; if (!sourceFile.exists() || sourceFile.isDirectory()) { return null; } // 或得到源文件当中的内容 并且将其复制进目的文件当中 sourceMsg = getSomeByFile(sourceFile.getAbsolutePath()); File destFile = new File(destName); // 如果这个目的地址不存在 则创建这个目的地址 if (!destFile.exists()) { destFile.createNewFile(); } if (destFile.isDirectory()) { return null; } // 向目的地址当中写入内容 FileOutputStream outputStream = new FileOutputStream(destFile); byte[] bytes = sourceMsg.getBytes("utf-8"); for (byte b : bytes) { outputStream.write(b); } outputStream.close(); return sourceMsg; } /** * 将指定地方的图片复制到目的地址,同上操作判断是否为空,返回boolean * <p> * 根据操作是否成功返回boolean * * @param sourceName * 原地址路径 * @param descName * 目的地址路径 * @return 返回操作是否成功 */ public boolean copyImageByName(String sourceName, String descName) { if (sourceName == null || descName == null) { return false; } File sourceFile = new File(sourceName); if (!sourceFile.exists() || sourceFile.isDirectory()) { return false; } File descFile = new File(descName); if (descFile.exists() || descFile.isDirectory()) { return false; } try { FileInputStream inputStream = new FileInputStream(sourceFile); FileOutputStream outputStream = new FileOutputStream(descFile); byte[] buffer = new byte[100]; int num = inputStream.read(buffer); while (num > 0) { outputStream.write(buffer, 0, num); num = inputStream.read(buffer); } inputStream.close(); outputStream.close(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } }}
阅读全文
0 0
- Java学习笔记(三)——文件读写操作
- Java文件操作(三)每次读写一行
- Androidx学习笔记(10)-- 文件读写操作简介
- Androidx学习笔记(11)-- 文件读写操作-在内部存储空间中读写文件
- Androidx学习笔记(12)-- 文件读写操作-在外部存储空间中读写文件
- PHP学习笔记——文件读写
- Python学习笔记——文件读写
- Android学习笔记(4)————Android的私人文件夹的文件读写操作
- Android学习笔记(11)————Android的私人文件夹的文件读写操作
- Android学习笔记(11)————Android的私人文件夹的文件读写操作 .
- QT sqlite3数据库读取、容器操作、文件读写——学习笔记。
- QT sqlite3数据库读取、容器操作、文件读写——学习笔记
- 文件读写操作笔记
- 容器与文件操作(三)文件的读写操作
- java多线程学习笔记——读写锁(ReentrantReadWriteLock)
- [perl学习笔记]文件操作之文件的读写
- [学习笔记]Java 中对文件的读写操作之比较
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用JXL生成Excel文件(一)
- 树状数组应用汇总(全)
- 实战详解win10专业版/企业版升级到win10 Enterprise G 企业政府版教程
- 【Android API】startActivity流程分析
- 1015. 德才论 (25)
- 【哈夫曼树,贪心】BZOJ4198 [Noi2015]荷马史诗
- Java学习笔记(三)——文件读写操作
- 使用JS 正则 匹配手机号码输入格式是否正确
- leetcode(110). Balanced Binary Tree
- Java List遍历的三种方式
- Java web入门——Servlet
- ROS下实现机器人序列任务的执行控制
- 455. Assign Cookies
- HDOJ 2066 一个人的旅行 (Dijkstra)
- AsyncStorage简析