java指定路径写、读文件

package com.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
* @ClassName: IOUtill
* @Description: I/O工具类
* @author 无名
* @date 2016-5-20 下午9:00:18
* @version 1.0
 */
public final class IOUtill
{
    private IOUtill(){}

    public static void writeByUrl(String url,String content)
    {
        File file = new File(url);
        if (!file.getParentFile().exists())
        {
           file.getParentFile().mkdirs();
        }
        try
        {
           file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
           FileWriter fw = new FileWriter(file, true);
           BufferedWriter bw = new BufferedWriter(fw);
           bw.write(content);
           bw.flush();
           bw.close();
           fw.close();
        } catch (IOException e) {
           e.printStackTrace();
        }
    }

    public static String readByUrl(String url)
    {
        File file = new File(url);
        if (!file.getParentFile().exists())
        {
           file.getParentFile().mkdirs();
        }
        String content = "";
        try
        {
            FileReader fr = new FileReader(file);
            BufferedReader bReader = new BufferedReader(fr);
            content = bReader.readLine();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }
}
时间: 2024-12-16 20:13:02

java指定路径写、读文件的相关文章

java:IO-读写大文件

import java.io.*; class Test { public static void main(String args[]){ FileInputStream fin =null; FileOutputStream fout = null; try{ fin = new FileInputStream("e://d/from.txt"); fout = new FileOutputStream("e://d/to.txt"); byte [] arr

C#遍历指定路径下的文件夹

通过指定路径访问路径下的文件,在C#的开发中主要利用了Directory类和DirectoryInfo类,简要介绍Directory类中的成员:命名空间 System.IO 命名空间 1.CreateDirectory,已重载,用于创建指定路径下的所有目录: 2.Delete,删除指定目录: 3.Exists,确定给定目录是否引用磁盘现有目录:说白点就是判断路径是否存在: 4.GetCreationTime,获取目录的创建时间和日期: 4.GetCurrentDirectory,获取应用程序的当

(IO流)java中多种方式读文件,追加文件内容,对文件的各种操作

import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.io.R

node.js+react全栈实践-Form中按照指定路径上传文件并

书接上回,讲到“使用同一个新增弹框”中有未解决的问题,比如复杂的字段,文件,图片上传,这一篇就解决文件上传的问题.这里的场景是在新增弹出框中要上传一个图片,并且这个上传组件放在一个Form中,和其他文本字段一起提交给接口. 这里就有几个要注意的问题: 图片上传时最好能在前端指定图片类型,根据这个类型上传到指定的目录.比如这里是新增用户,上传用户图片,那么这里就指定类型是“user”,那么就把这个文件上传到服务器的upload/user目录中.这样方便后期维护,比如要把项目中的文件统一迁移到另外一

Java递归输出指定路径下所有文件及文件夹

package a.ab; import java.io.File; import java.io.IOException; public class AE { public static void main(String[] args) { File f=new File("D:\\DD"); new AE().fileList(f); } public void fileList(File fl){ try{ File[] fs=fl.listFiles(); for(File f

JAVA用流在指定路径下生成文件

// 待处理的流 ByteArrayOutputStream bao = new ByteArrayOutputStream(); // 定义文件根路径:TOMCAT的temp路径 + 时间戳 String baseDir = System.getProperty("java.io.tmpdir")  + System.currentTimeMillis(); //待输出文件流对象 FileOutputStream fos = null; try { // 自定义文件路径 String

java打印出某一指定路径下的文件夹内的所有子文件夹和文件,并区分开来

public class printoutFile { public static void main(String[] args) { printFile(new File("D:\\test"),1); } public static void printFile(File file,int sub) { if (file.isFile()) { System.out.println("您给定的是一个文件"); // 判断给定目录是否是一个合法的目录,如果不是,

java使用POI写Excel文件

参考地址:http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html 1 jar包 网上下载 2 源代码 package zjr.amy.excel; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java

linux-在指定路径下查询文件夹是否存在

我们常常在Linux下去查找文件 find / -name 'test.py' # 在根目录下查找名为test.py的文件 但是如果用查找文件的方式去查找文件夹的话,是查不到的 find / -maxdepth 1 -type d -name 'test_dictionary' # -maxdepth表示搜索深度.-type表示搜索类型. 查找成功返回相应路径 原文地址:https://www.cnblogs.com/lutt/p/12037498.html