package MOdifyLyrics;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import javax.swing.JFileChooser;
/**
* 修改歌词 -用字符流进行修改 再将其封装陈方法进行创建
* @author 李。文
* @version 1.8
* @date 2019年8月4日 上午11:10:49
* @content JAVA代码
* @motto 代码千万条,可读第一条。代码不规范,error多两行。
*/
public class ChangeLyrics {
public static final String filePath = "c://test";
/**
* 对歌词内容进行写入
* @return 歌词字符串
* @throws IOException
*/
public static void fileInput()
{
String content=null;
String filePath="c:\\test";//文件夹所在的路径
JFileChooser filechoose=new JFileChooser(filePath);
filechoose.showOpenDialog(null);
File file=filechoose.getSelectedFile();
InputStream inputStream = null;
BufferedInputStream binStream=null;
int size = 0;
try {
inputStream = new FileInputStream(file);
binStream=new BufferedInputStream(inputStream);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
size = binStream.available();
} catch (IOException e1) {
e1.printStackTrace();
}
byte[]bytes=new byte[size];
try {
binStream.read(bytes);
} catch (IOException e) {
e.printStackTrace();
}//进行字节的读取 将读取的字节赋值给字节数组
content=new String(bytes);
System.out.println("文件"+file.getName()+"中的内容为:");
System.out.println(content);
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
binStream.close();
} catch (IOException e) {
e.printStackTrace();
}
fileOutput(content);
}
public static void fileOutput(String content)
{
//String content =null;
String newContent=null;
JFileChooser fileChooser=new JFileChooser(filePath);
fileChooser.showOpenDialog(null);
File file=fileChooser.getSelectedFile();
OutputStream outputStream=null;
BufferedOutputStream out=null;
newContent=content.replaceAll("男:", "男--"). replaceAll("女:", "女--").replaceAll("(合)", "合--");
System.out.println();
System.out.println(newContent);
byte[]bytes=newContent.getBytes();
try {
outputStream=new FileOutputStream(file,false);
out =new BufferedOutputStream(outputStream);
//content=fileInput();
//bytes=content.getBytes();
//对文件内容 进行写入操作
try {
out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("歌词修改成功!");
}
public static void main(String[] args) throws IOException {
//调用读取文件的函数
fileInput();
// // 用字符流进行解决
// JFileChooser fileChoose = new JFileChooser(filePath);
// fileChoose.showOpenDialog(null);
// File file = fileChoose.getSelectedFile();
// Reader reader = null;
// BufferedReader bReader = null;
// String line = null;
// StringBuffer content = new StringBuffer();
// String lyrics = null;
// try {
// reader = new FileReader(file);
// bReader = new BufferedReader(reader);
// // 进行歌词的读取
// try {
// while ((line = bReader.readLine()) != null) {
// content.append(line);
// content.append(System.getProperty("line.separator"));
// }
//
// lyrics = content.toString().replaceAll("男:", "男--").replaceAll("女:",
// "女--").replaceAll("(合)", "合--");
//
// } catch (IOException e) {
// e.printStackTrace();
// }
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } finally { // 对字符流进行关闭
// try {
// bReader.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// try {
// reader.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// // 进行文件的内容的写入操作
// Writer writer = null;
// BufferedWriter bWriter = null;
// try {
//
// writer = new FileWriter(file);
// bWriter = new BufferedWriter(writer);
// bWriter.write(lyrics);
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// try {
// bWriter.flush();
// } catch (IOException e) {
//
// e.printStackTrace();
// }
// try {
// bWriter.close();
// } catch (IOException e) {
//
// e.printStackTrace();
// }
// try {
// writer.close();
// } catch (IOException e) {
//
// e.printStackTrace();
// }
// System.out.println("歌词修改完毕!");
// }
}
}