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;
}

最新文章

  1. Web Service和WCF的区别。其实二者不属于一个范畴!!!
  2. ABAP 客户报表
  3. TCP重传率高的监控
  4. Django admin美化插件suit
  5. Windows 2008 R2 X64 安装WebsitePanel(WSP虚拟主机管理面板)
  6. 【转】Android屏幕适配全攻略(最权威的官方适配指导)
  7. HDU 3577 Fast Arrangement (线段树区间更新)
  8. Deadline来了,如何按时结题?
  9. EdasStudio 开发工具用户手册
  10. vb.net转换为C#方法
  11. AllocConsole
  12. poj2531
  13. 苹果手表的真实触感信息(Real Touch Messaging)
  14. 【Flex】去除外边框,底背景透明,改变exe的icon
  15. 微信小程序(三)页面跳转和图片滑动切换
  16. Salt Document学习笔记1
  17. BZOJ1150 [CTSC2007]数据备份Backup 贪心 堆
  18. Oracle relink 重新编译
  19. ASP.NET 文件上传于下载
  20. sass,less的安装及sass的教程

热门文章

  1. 用javascript动态改变网页文字大小
  2. Linux环境Perl链接MS Sql Server数据库
  3. Visual Studio 2017 新特性
  4. javascript数组详解(js数组深度解析)【forEach(),every(),map(),filter(),reduce()】
  5. 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
  6. 移动端ios 输入框fixed固定在底部 焦点时乱跳加遮盖问题的解决 转自zhangyunling 加个人项目解决方案
  7. JSP九大内置对象的作用和用法总结(转)
  8. Ubuntu下php网站运行环境搭建
  9. 深度学习实践系列(3)- 使用Keras搭建notMNIST的神经网络
  10. llinux svn安装