java读取txt 格式: 0 8584gh vhh558 三个数据存入数组 有4000行这样的数据
1个回答
展开全部
//写了一会儿呢。。给点辛苦分吧
package com.secondDay;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class Test1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test1().getFile("D:\\haha.txt","D:\\WUWUWU.txt");
}
public void getFile(String path,String path1){
File file = new File(path);
FileInputStream fis = null;
String readStr;
String[] str = null;
StringBuffer sb = new StringBuffer();
StringBuffer sb1 = new StringBuffer();
if(file.exists()){
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while((readStr=br.readLine())!=null){
sb.append(readStr+"\n");
}
System.out.println(sb.toString());
str = sb.toString().split("\n");
if(null==str){
throw new Exception("获取异常!");
}
for(int i=0;i<str.length;i++){
String[] chgStr = str[i].split(",");
sb1.append(chgStr[0]+"Z\n");
}
System.out.println(sb1.toString());
this.write(sb1.toString(), path1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
throw new Exception("文件不存在!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void write(String text,String path){
FileOutputStream fos = null;
if(!"".equals(text.trim())){
try {
fos = new FileOutputStream(path);
fos.write(text.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.secondDay;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class Test1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test1().getFile("D:\\haha.txt","D:\\WUWUWU.txt");
}
public void getFile(String path,String path1){
File file = new File(path);
FileInputStream fis = null;
String readStr;
String[] str = null;
StringBuffer sb = new StringBuffer();
StringBuffer sb1 = new StringBuffer();
if(file.exists()){
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while((readStr=br.readLine())!=null){
sb.append(readStr+"\n");
}
System.out.println(sb.toString());
str = sb.toString().split("\n");
if(null==str){
throw new Exception("获取异常!");
}
for(int i=0;i<str.length;i++){
String[] chgStr = str[i].split(",");
sb1.append(chgStr[0]+"Z\n");
}
System.out.println(sb1.toString());
this.write(sb1.toString(), path1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
throw new Exception("文件不存在!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void write(String text,String path){
FileOutputStream fos = null;
if(!"".equals(text.trim())){
try {
fos = new FileOutputStream(path);
fos.write(text.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询