62,628
社区成员
发帖
与我相关
我的任务
分享

import java.io.*;
public class SongList {
FileInputStream in = null;
FileOutputStream out = null;
int b = 0;
public static void main(String[] args) {
SongList s = new SongList();
s.go();
}
public void go(){
try{
in = new FileInputStream("c:/songlist.txt");
out = new FileOutputStream("C:/songlist(1).txt");
while ((b = in.read()) != -1){
String song = in.read()+"";
String[] s =song.split(",");
String k = s.toString();
byte[] m = k.getBytes();
out.write(m);
}
System.out.println("succeed!");
}catch(FileNotFoundException fe){
System.out.println("not found");
}catch (IOException ie) {
System.out.println("IOException");
}
}
}


package com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
readTxt("E:\\test\\test0.txt");
}
/**
* 读取txt文本
*
* @param path
*/
public static void readTxt(String path) {
System.out.println("读取的文件路径为:"+path);
File file = new File(path);
BufferedReader reader = null;
String tempString = null;
StringBuilder result = new StringBuilder();
try {
reader = new BufferedReader(new FileReader(file));
while ((tempString = reader.readLine()) != null) {
if (!tempString.isEmpty()) {
result.append(tempString.replaceAll(",", "\r\n")).append("\r\n");
}
}
writeText(result.toString(), "E:\\test\\test.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 写入文件
* @param text
* @param path
*/
public static void writeText(String text, String path) {
System.out.println("保存的文件路径为:"+path);
FileWriter fileWrite = null;
try {
fileWrite = new FileWriter(path);
fileWrite.write(text);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileWrite.flush();
fileWrite.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
