java怎样从txt文件中有选择的读取数据存入数组,且每个数据项(有汉字或数字)之间用逗号隔开?
数据格式如下:第一行:AA,234.4,13,478.345第二行:wh,432,78654,45.56第三行……有很多行,每行第一个逗号之前为字符,第二个逗号往后为数字...
数据格式如下:第一行:AA,234.4,13,478.345 第二行:wh,432,78654,45.56 第三行……有很多行,每行第一个逗号之前为字符,第二个逗号往后为数字,逗号分割。
将读取的每一个单独的字符加上字母后,写到c:\test.txt中。谢谢,再谢谢
是c++写错了 展开
将读取的每一个单独的字符加上字母后,写到c:\test.txt中。谢谢,再谢谢
是c++写错了 展开
展开全部
//写了一会儿呢。。给点辛苦分吧
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();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询