java文件的读写操作主要是对输入流和输出流的操作,由于流的分类很多,所以概念很容易模糊,基于此,对于流的读写操作做一个小结。

  1、根据数据的流向来分:
    输出流:是用来写数据的,是由程序(内存)--->外界设备
    输入流:是用来读数据的,是由外界设备--->程序(内存)
    如何区分:一般来说输入流带有Input,输出流带有Output

  2、根据流数据的格式来分:
    字节流:处理声音或者图片等二进制的数据的流,比如InputStream
    字符流:处理文本数据(如txt文件)的流,比如InputStreamReader
    如何区分:可用高低端流来区分,所有的低端流都是字节流,所有的高端流都是字符流

                 

/**
* @Description: 获得控制台用户输入的信息
*/
public String getInputMessage() throws IOException{
System.out.println("请输入您的命令∶");
byte buffer[]=new byte[1024];
int count=System.in.read(buffer);
char[] ch=new char[count-2];//最后两位为结束符,删去不要
for(int i=0;i<count-2;i++)
ch[i]=(char)buffer[i];
String str=new String(ch);
return str;
}

/**
*
*
* @param srcFile 文件地址 例如:"f:" + File.separator + "testoyy"+File.separator+"test2.txt"
* @param content 写入文件的内容
* @throws Exception
*
* @Description: 将内容写入指定文件
*/
public void writeToTxtFile(String srcFile, String content) throws Exception{
File file = new File(srcFile) ;
//判断文件是否存在
if (!file.exists() != false)
{
file.createNewFile();
}
Writer out = null ; // 字符输出流
out = new OutputStreamWriter(new FileOutputStream(file)) ; // 字节流变为字符流
out.write(content) ; // 使用字符流输出
out.close() ;
}

/**
* 指定编码格式写文件
*
* @param srcFile 读取该文件的内容的地址 例如:"F:\\testoyy\\test.txt";
* @param srcCode 读取文件的编码 例如: "utf-8";
* @param distFile 写入文件内容的地址 例如:"F:\\testoyy\\test1.txt";
* @param distCode 写入文件的编码 例如: "utf-8";
* @return
* @throws Exception
*
* @Description: 指定编码格式将一个文件的内容复制到另外一个文件中
*/
public boolean writeToTxtFile(String srcFile, String srcCode,
String distFile, String distCode) throws Exception
{
//输出流
Writer writer = null;
try
{
File dist_File = new File(distFile);
//判断文件是否存在
if (!dist_File.exists() != false)
{
dist_File.createNewFile();
}
writer = new OutputStreamWriter(new FileOutputStream(dist_File),
"GBK");
File src_File = new File(srcFile);
//输入流
InputStreamReader read = new InputStreamReader(new FileInputStream(
src_File));
BufferedReader reader = new BufferedReader(read);
String line;
//逐行读取
while ((line = reader.readLine()) != null)
{
//写入文件内
writer.write(line);
}
} catch (Exception e)
{
// EmpExecutionContext.error(e,"指定编码格式写文件异常!");
throw e;
} finally
{
//关闭流
writer.close();
}
//返回结果
return true;
}

/**
* 以字符为单位读取文件
* @param fileName 文件地址 例如:"f:" + File.separator + "testoyy"+File.separator+"test2.txt"
* @return
*/
public String readFileByChars(String fileName)
{
File file = new File(fileName);
StringBuffer fileContent=new StringBuffer();
Reader reader = null;
try
{
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
// 一次读一个字节
while ((tempchar = reader.read()) != -1)
{
fileContent.append((char)tempchar);

}
//关闭流
reader.close();
} catch (Exception e)
{
//异常信息打印
//EmpExecutionContext.error(e,"以字符为单位读取文件异常!");
}finally
{
if (reader != null)
{
try
{
//关闭流
reader.close();
} catch (IOException e1)
{
//异常信息打印
//EmpExecutionContext.error(e1,"关闭流异常");

}
}
}
//返回读取到的数据
return fileContent.toString();
}

/**
* 读取第一行数据
* @param fileName
* @return
*/
public String readFileFirstLine(String fileName)
{
File filee = new File(fileName);
String tempString = null;
BufferedReader brr = null;
try
{
brr = new BufferedReader(new FileReader(filee));
tempString = brr.readLine();
brr.close();
} catch (IOException e)
{
//异常信息打印
//EmpExecutionContext.error(e,"读取文件第一行数据异常!");
} finally
{
if (brr != null)
{
try
{
//关闭流
brr.close();
} catch (IOException ioe)
{
//EmpExecutionContext.error(ioe,"文件流关闭异常!");
}
}
}
//返回数据
return tempString;
}

/**
* 以行为单位读取文件
* @param fileName 文件路径名 例如:"f:" + File.separator + "testoyy"+File.separator+"test2.txt"
* @return 文件内容
*/
public String readFileByLines(String fileName)
{
String phoneStr = null;
BufferedReader br = null;
try
{
File file = new File(fileName);
StringBuffer sb = new StringBuffer();
br = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = br.readLine()) != null)
{
//将读取到的数据添加到stringBuffer
sb.append(tempString.trim()).append(",");
}
//截取字符串
if (sb.lastIndexOf(",") != -1)
{
sb.deleteCharAt(sb.lastIndexOf(","));
}
phoneStr = sb.toString();
sb.setLength(0);
} catch (Exception e)
{
//异常信息打印
// EmpExecutionContext.error(e,"以行为单位读取文件异常!");
} finally
{
try
{
if (br != null)
{
//关闭流
br.close();
}
} catch (IOException ioe)
{
//异常信息打印
// EmpExecutionContext.error(ioe,"关闭文本流异常! ");
}
}
//返回读到的数据
return phoneStr;
}

java文件的读写操作的更多相关文章

  1. java 文件的读写操作

    java  文件的读写操作 一.读: public String getSetting() { HttpServletRequest request=org.apache.struts2.Servle ...

  2. Java 对不同类型的数据文件的读写操作整合器[JSON,XML,CSV]-[经过设计模式改造](2020年寒假小目标03)

    日期:2020.01.16 博客期:125 星期四 我想说想要构造这样一个通用文件读写器确实不容易,嗯~以后会添加更多的文件类型,先来熟悉一下文件内容样式: <?xml version=&quo ...

  3. android报错及解决2--Sdcard进行文件的读写操作报的异常

    报错描述: 对Sdcard进行文件的读写操作的时候,报java.io.FileNotFoundException: /sdcard/testsd.txt (Permission denied),在往S ...

  4. 使用字符流(Writer、Reader)完成对文件的读写操作

    字符流 字符输出流:Writer,对文件的操作使用子类FileWriter 字符输入流:Reader,对文件的操作使用子类FileReader 每次操作的是一个字符 文件字符操作流会自带缓存,默认大小 ...

  5. INI 文件的读写操作

    在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下: #region 变量 private static r ...

  6. Android 对 properties文件的读写操作

    -. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...

  7. C++学习48 对ASCII文件的读写操作

    如果文件的每一个字节中均以ASCII代码形式存放数据,即一个字节存放一个字符,这个文件就是ASCII文件(或称字符文件).程序可以从ASCII文件中读入若干个字符,也可以向它输出一些字符. 对ASCI ...

  8. Delphi- ini文件的读写操作

    一.读INI文件示例 procedure TForm1.FormCreate(Sender: TObject); Var MyIni :Tinifile; glAppPath :string; beg ...

  9. python使用装饰器对文件进行读写操作'及遍历文件目录

    '''使用装饰器对文件进行读写操作''' # def check_permission(func): # '''演示嵌套函数定义及使用''' # def wrapper(*args,**kwargs) ...

随机推荐

  1. 百度推出 MIP Shell 链接

    在站长将站点 MIP 化时,需要关注 URL 的一共有三个:MIP URL, MIP-Cache URL 以及 MIP-Shell URL. 从 URL 说起 在互联网中,URL 定义页面的地址,每个 ...

  2. deepin系统如何安装deb格式的软件

    很简单,命令如下: sudo dpkg -i *.deb 记得路径要对

  3. Jmeter新建用例图示

    添加线程组   添加HTTP请求   编辑HTTP请求 添加HTTP信息头   编辑HTTP信息头 添加断言   添加查看结果树   添加聚合报告   添加响应时间   添加TPS   批量运行命令: ...

  4. Java单例模式再加强——按组多单例

    最近要使用alibaba的rocket mq(我们公司对其进行了封装,使其运行在dotNet平台上,Java还是和原生的差不多,涉及公司的内容本文不会提及),其中 在生产者组这一块,建议是用单例模式的 ...

  5. MongoDB基础教程系列--第一篇 进入MongoDB世界

    1.什么是MongoDB MongoDB是跨平台的.一个基于分布式文件存储的数据库.由C++语言编写.用它创建的数据库具备性能高.可用性强.易于扩展等特点.MongoDB将数据存储为一个文档,数据结构 ...

  6. 【Egret】Native版本 视频播放器(android)

    前段时间,领导说客户要一个平板版本的视频播放器,把我们做的一些视频资源放进去,要是本地的:我们部门又没有app开发程序员,正好又前段我在实验egret的app打包功能,就说用egret做(ps:本来想 ...

  7. 老李分享:SSL协议相关证书

    老李分享:SSL协议相关证书   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:9088214 ...

  8. 老李分享:loadrunner用javavuser进行接口测试

    老李分享:loadrunner用javavuser进行接口测试 在这里分享一个poptest培训过程中案例,在日常工作中会遇到被测试系统通讯都是通过加密的数据包,加密算法是公司自己开发的,并且发送的数 ...

  9. 老李推荐:第5章7节《MonkeyRunner源码剖析》Monkey原理分析-启动运行: 循环获取并执行事件 - runMonkeyCycles

    老李推荐:第5章7节<MonkeyRunner源码剖析>Monkey原理分析-启动运行: 循环获取并执行事件 - runMonkeyCycles   poptest是国内唯一一家培养测试开 ...

  10. 老李推荐:第1章3节《MonkeyRunner源码剖析》概述:架构

    老李推荐:第1章3节<MonkeyRunner源码剖析>概述:架构   原理架构 MonkeyRunner使用起来非常的简单,只需要导入以下几个类基本上就能满足测试脚本编写的需求,比如: ...