package com.abin.facade.ws.mail.function;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.RandomAccessFile; public class FileOperation { /**
* 创建文件
* @param fileName
* @return
*/
public static boolean createFile(File fileName)throws Exception{
boolean flag=false;
try{
if(!fileName.exists()){
fileName.createNewFile();
flag=true;
}
}catch(Exception e){
e.printStackTrace();
}
return true;
} /**
* 读TXT文件内容
* @param fileName
* @return
*/
public static String readTxtFile(File fileName)throws Exception{
String result=null;
FileReader fileReader=null;
BufferedReader bufferedReader=null;
try{
fileReader=new FileReader(fileName);
bufferedReader=new BufferedReader(fileReader);
try{
String read=null;
while((read=bufferedReader.readLine())!=null){
result=result+read+"\r\n";
}
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(bufferedReader!=null){
bufferedReader.close();
}
if(fileReader!=null){
fileReader.close();
}
}
System.out.println("读取出来的文件内容是:"+"\r\n"+result);
return result;
} public static boolean writeTxtFile(String content,File fileName)throws Exception{
RandomAccessFile mm=null;
boolean flag=false;
FileOutputStream o=null;
try {
o = new FileOutputStream(fileName);
o.write(content.getBytes("GBK"));
o.close();
// mm=new RandomAccessFile(fileName,"rw");
// mm.writeBytes(content);
flag=true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(mm!=null){
mm.close();
}
}
return flag;
} public static void contentToTxt(String filePath, String content) {
String str = new String(); //原有txt内容
String s1 = new String();//内容更新
try {
File f = new File(filePath);
if (f.exists()) {
System.out.print("文件存在");
} else {
System.out.print("文件不存在");
f.createNewFile();// 不存在则创建
}
BufferedReader input = new BufferedReader(new FileReader(f)); while ((str = input.readLine()) != null) {
s1 += str + "\n";
}
System.out.println(s1);
input.close();
s1 += content; BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace(); }
} }

java从txt文档读写数据的更多相关文章

  1. 整理关于Java进行word文档的数据动态数据填充

    首先我们看下,别人整理的关于Java生成doc 的 资料. java生成word的几种方案 1. Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用 ...

  2. 使用Aspose.word (Java) 填充word文档数据(包含图片填充)

    Aspose填充word数据 本文介绍了如何使用aspose进行word文档的生成,并提供了工具类供参考. 有问题欢迎 call 微信:905369866,小弟尽力而为..毕竟这玩意没吃透. 目录 A ...

  3. java简单实现用语音读txt文档

    最近比较无聊,随便翻着博客,无意中看到了有的人用VBS读文本内容,也就是读几句中文,emmm,挺有趣的,实现也很简单,都不需要安装什么环境,直接新建txt文件,输入一些简单的vbs读文本的代码,然后将 ...

  4. 用matlab查找txt文档中的关键字,并把关键字后面的数据存到起来用matlab处理

    用matlab查找txt文档中的关键字,并把关键字后面的数据存到起来用matlab处理 我测了一组数据存到txt文件中,是个WIFI信号强度文档,里面有我们需要得到的数据,有没用的数据,想用matla ...

  5. 一个简易的Python爬虫,将爬取到的数据写入txt文档中

    代码如下: import requests import re import os #url url = "http://wiki.akbfun48.com/index.php?title= ...

  6. Java 将 List 里面的内容写入桌面的 txt 文档

    文件内容: /** * 将 list 写入文件中 */ import java.io.BufferedWriter; import java.io.File; import java.io.FileW ...

  7. 用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)

    前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中.数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过 ...

  8. java通过url在线预览Word、excel、ppt、pdf、txt文档

    java通过url在线预览Word.excel.ppt.pdf.txt文档中的内容[只获得其中的文字] 在页面上显示各种文档中的内容.在servlet中的逻辑 word: BufferedInputS ...

  9. VBS读取txt文档数据查找Excel中单元格数据符合条件的剪切到工作表2中

    Dim fso,f,a set oExcel = CreateObject( "Excel.Application" ) oExcel.Visible = false '4) 打开 ...

随机推荐

  1. orchard cms 项目迁移

    删除Orchard.Web  下的 App_Data 目录,重新安装项目

  2. 虚拟机Mac系统中VMware_tools安装和vm共享文件夹的设置(转)

    原文来源: http://wenku.baidu.com/link?url=KRgfG40q2SEwZfde9xA7HVKjCsFBkMcf83tyellnzsHYZ_ErU1hWpVmTHYZem0 ...

  3. 74. Search a 2D Matrix (Graph; Divide-and-Conquer)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  4. transform.rotation和GetComponent<Rigidbody>().MoveRotation

    同时在UPDATE和FIXED UPDATE中调整 旋转 并未出现闪,而是一直以UPDATE中的为准,可认为MoveRotation调用后在UPDATE中生效 using System.Collect ...

  5. 在Action中操作域对象

    ----------------------siwuxie095 在 Action 中操作域对象 1.在 Action 中可以操作的域对象主要有三个: (1)Request (2)Session (3 ...

  6. Ubuntu18.04安装Virtualenv虚拟环境

    在Ubuntu18.04安装Virtualenv虚拟环境 [实验环境]: 在这台电脑上已经安装了python3 [安装参考] 1.查看是否已安装virtualenv virtualenv --vers ...

  7. qt QTcpServer与QTcpSocket通讯

    分类: C/C++ TCP        TCP是一个基于流的协议.对于应用程序,数据表现为一个长长的流,而不是一个大大的平面文件.基于TCP的高层协议通常是基于行的或者基于块的.          ...

  8. [Training Video - 1] [Selenium Basics] [What is Selenium IDE,RC,Webdriver, TestNG, Junit And Ant]

    Selenium IDE (Only support in Firefox): - Record and Run - UI interface - User extensions - Conversi ...

  9. [Training Video - 5] [Groovy Script Test Step - Collections, Exceptions] HashSet and Hashtable

    Hashset: HashSet set = new HashSet() set.add("India") set.add("USA") set.add(&qu ...

  10. UI7Kit

    [UI7Kit] UI7Kit is a GUI toolkit which can backport flat-style UIKit from iOS7 to iOS5/iOS6. Additio ...