javatxt文件处理的简单介绍
今天给各位分享javatxt文件处理的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java 处理TXT文件问题
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class T3 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File sourceFile=new File("D:\\1.txt");//原来的txt文件
File desFile=new File("D:\\2.txt");//目标txt文件,程序运行会自动创建
BufferedReader buffer=new BufferedReader(new FileReader(sourceFile));
PrintWriter pw=new PrintWriter(new FileWriter(desFile));
String s="";
while((s=buffer.readLine())!=null){
//System.out.println(s);
if(s.length()0){
String[] ss=s.split(" ");
String temp=ss[0];
ss[0]=ss[2];
ss[2]=temp;
pw.println(ss[0]+" "+ss[1]+" "+ss[2]);
}
}
pw.close();
buffer.close();
}
}
java读取、修改、写入txt文件
模拟:先创建一个TXT文件(内容来自控制台);然后读取文件并在控制台输出;最后实现对新创建的TXT文件(的数据进行排序后)的复制。分别对应三个函数,调用顺序需要注意:创建、读取、复制。
效果图如下:绿色部分为控制台输入的内容(当输入end时,结束)
代码如下:
package com.baidu;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;
public class CreateAndReadTxt {
// 文件名称
public static String fileName = ".txt";
public static String newFileName = ".txt";
// 文件路径
public final static String URL = System.getProperty("user.dir");
// CreateAndReadTxt.class.getResource("/").getPath();
// 创建TXT文件
public static void createTxtFile(String fName, String fileContent) {
// 创建文件
fileName = fName + fileName;
File file = new File(fileName);
// 可以更改
file.setWritable(true);
// 判断当前路径下是否存在同名文件
boolean isExist = file.exists();
if (isExist) {
// 文件存在,删除
file.delete();
}
// 写入文件
try {
// 文件写入对象
FileOutputStream fos = new FileOutputStream(file);
// 输入流写入----默认字符为GBK
OutputStreamWriter osw = new OutputStreamWriter(fos);
// 写入
osw.write(fileContent);
// 写入完毕后关闭
osw.close();
System.out.println("成功创建文件:\t"+fileName);
} catch (IOException e) {
System.out.println("写入文件失败:\t" + e.getMessage());
}
}
// 阅读文件
public static void readFile(String fileName) {
System.out.println("开始读取文件:\t" + fileName);
// 产生文件对象
File file = new File(fileName);
//
try {
// 字符读取
FileReader fr = new FileReader(file);
// 缓冲处理
BufferedReader br = new BufferedReader(fr);
String str = "";
while ((str = br.readLine()) != null) {
System.out.println(str);
}
// 关闭
br.close();
fr.close();
} catch (FileNotFoundException e) {
System.out.println("读取文件失败:\t" + e.getMessage());
} catch (IOException e) {
System.out.println("读取文件失败:\t" + e.getMessage());
}
}
// 文件复制
public static void copyFile(String fromFileName,String toFileName){
//读取文件
File file = new File(fromFileName);
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
// 定义接收变量
VectorDouble vec = new VectorDouble();
String s = "";
while(null!=(s=br.readLine())){
vec.add(Double.parseDouble(s));
}
br.close();
fr.close();
// 保存到数组并进行排序
Double dou[] = new Double[vec.size()];
vec.toArray(dou);
Arrays.sort(dou);
System.out.println("========复制文件=========");
// 写入新文件
newFileName = "副本"+newFileName;
File newFile = new File(toFileName);
FileOutputStream fos = new FileOutputStream(newFile, true);
OutputStreamWriter osm = new OutputStreamWriter(fos);
for(Double d:dou){
osm.write(d.doubleValue()+"\n");
}
osm.close();
fos.close();
} catch (FileNotFoundException e) {
System.out.println("读取文件失败:\t" + e.getMessage());
} catch (IOException e) {
System.out.println("读取文件失败:\t" + e.getMessage());
}
}
public static void main(String[] args) {
/**
* 构造数据
*/
Scanner scan = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
String s = "";
while(!("end".equals(s = scan.next()))){// 当输入end时,结束
sb.append(s);
sb.append("\n");
}
scan.close();
/**
* 使用数据
*/
CreateAndReadTxt.createTxtFile("creat", sb.toString());
CreateAndReadTxt.readFile(fileName);
System.out.println(fileName);
CreateAndReadTxt.copyFile(fileName, newFileName);
CreateAndReadTxt.readFile(newFileName);
}
}
java读取txt的数据并处理
直接给你一段实际可以用的代码吧
/**
* 读取一行一行的文件
*
* @param filename
* @return
* @throws IOException
*/
public static ListString readLinedFile(String filename){
ListString filecon = new ArrayListString();
String m = "";
BufferedReader file = null;
try{
file = new BufferedReader(new FileReader(filename));
while ((m = file.readLine()) != null) {
if (!m.equals("")) // 不需要读取空行
{
filecon.add(m);
}
}
file.close();
}catch(IOException e){
e.printStackTrace();
}
return filecon;
}
这段代码实现了,你传入文件的路径,给你读出一个String的List,一个String就是文件的一行,拿到这个List,你爱怎么处理就怎么处理,就比如你现在这个需求吧,就把这个List拿来遍历
//这个是去除不含genotype字符串后的数组
ListString resultList = new ArrayListString();
for(String line:filecon){
if(line.indexOf("genotype") != -1){
resultList.add(line);
}
}
这样处理完后就拿到了一个只有含有 genotype的数组,然后剩下的就是把这个数组写到一个文件里面,我给你看一个把字符串数组写到文件的方法,你可以拿去直接用
/**
* 写入一行一行的文件
* @param lst 要写入的字符串数组
* @param filename 要写入的文件路径
* @return
* @throws IOException
*/
public static void writeLinedFile(List lst, String filePath) throws IOException {
File file = new File(filePath);
BufferedWriter out = new BufferedWriter(new FileWriter(file, false));
for (int i = 0; i lst.size(); i++) {
String temp = (String) lst.get(i);
if (!StringUtils.isBlank(temp)) {
out.write(temp);
out.newLine();
}
}
out.close();
out = null;
file=null;
}
这样懂了吧?
求java操作txt文件的方法
/**
* @date:2015年5月11日 上午9:58:42
* @Description:读取指定行内容,不包括空行
* @param n
* @return
*/
public String _read(int n) throws Exception {
ListString list = new ArrayListString();
BufferedReader b = null;
File file = new File("C:\\Users\\Administrator\\Desktop\\远程调用.txt");
b = new BufferedReader(new FileReader(file));
String line = null;
while ((line = b.readLine()) != null) {
if (!line.equals("")) {
list.add(line);
}
}
b.close();
return list.get(n - 1);
}
/**
* @date:2015年5月11日 上午11:54:16
* @Description:修改指定行的指定数组内容
* @param n
* @param str
* @throws Exception
*/
public void _change(int n, String[] str) throws Exception {
File file = new File("C:\\Users\\Administrator\\Desktop\\远程调用.txt");
BufferedReader b=new BufferedReader(new FileReader(file));
StringBuffer sb = new StringBuffer();
StringBuffer sb1= new StringBuffer();
for (int i = 0; i str.length; i++) {
sb.append(str[i]);
}
String line = null;
int index=1;
while ((line = b.readLine()) != null) {
if (!line.equals("")index!=n) {
sb1.append(line);
sb1.append(System.getProperty("line.separator"));
index++;
}else if(!line.equals("")index==n){
sb1.append(sb.toString());
sb1.append(System.getProperty("line.separator"));
index++;
}
}
b.close();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(sb1.toString());
bw.close();
}
java处理txt文件
分别读取两个文件
String[] s1 = new String[] { "1,admin", "2,manager", "3,qyi" };//假设这是第一个文件
String[] s2 = new String[] { "SN222,admin", "SN33,manager","SN0982,qyi" };//第二个文件的内容
ListString l = new ArrayListString();//比较组合后的结果集
for (int i = 0; i s1.length; i++) {//取第一文件的每一个对象与第二文件的每一个对象
String[] ss1 = s1[i].split(",");
for (int j = 0; j s2.length; j++) {
String[] ss2 = s2[j].split(",");
if(ss2[1].equals(ss1[1])){//根据用户名进行比较,如果相同的话就进行字符串拼接,然后放到结果集当中,
l.add(ss1[0]+","+ss2[0]+","+ss1[1]);
continue;//跳出内循环
}
}
}
for(String s : l){//打印结果集(可以在这里将结果集写入到新的文件当中去)
System.out.println(s);
}
javatxt文件处理的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javatxt文件处理的信息别忘了在本站进行查找喔。
发布于:2022-12-02,除非注明,否则均为
原创文章,转载请注明出处。