请用JAVA编一个小程序对文本文件进行编辑,能运行给200分
请用java编写一个小程序,读入一个文本文件,格式如下:CN=aa,EMAILADDRESS=bb,OU=cc,O=dd,C=eeCN=ff,EMAILADDRESS=g...
请用java编写一个小程序,读入一个文本文件,格式如下:
CN=aa, EMAILADDRESS=bb, OU=cc, O=dd, C=ee
CN=ff, EMAILADDRESS=gg, OU=hh, O=dd, C=ii
.........................................
若干行,把格式进行修改,修改结果如下
EMAIL=bb, CN=aa, OU=cc, O=dd, C=ee
EMAIL=gg, CN=ff, OU=hh, O=dd, C=ii
...........................................
请注意每一个“,”后面都有空格
“EMAILADDRESS”变成“EMAIL”
把修改结果存成一个新的文本文件。
非常感谢 展开
CN=aa, EMAILADDRESS=bb, OU=cc, O=dd, C=ee
CN=ff, EMAILADDRESS=gg, OU=hh, O=dd, C=ii
.........................................
若干行,把格式进行修改,修改结果如下
EMAIL=bb, CN=aa, OU=cc, O=dd, C=ee
EMAIL=gg, CN=ff, OU=hh, O=dd, C=ii
...........................................
请注意每一个“,”后面都有空格
“EMAILADDRESS”变成“EMAIL”
把修改结果存成一个新的文本文件。
非常感谢 展开
5个回答
展开全部
import java.io.IOException;
import java.io.RandomAccessFile;
public class TestMain {
public static void main(String[] args) {
//原文件路径
String path1 = "F:/1.txt";
//新文件路径
String path2 = "F:/2.txt";
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
String line = null;
try {
raf1 = new RandomAccessFile(path1, "r");
raf2 = new RandomAccessFile(path2, "rw");
while ((line = raf1.readLine()) != null) {
int start = line.indexOf("EMAIL");
int end = line.indexOf("OU=");
String a = line.substring(start, end);
String b = a.replace("ADDRESS", "");
line = line.replace(a, "");
line = b + line;
raf2.write(line.getBytes());
raf2.write('\n');
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
raf1.close();
raf2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.RandomAccessFile;
public class TestMain {
public static void main(String[] args) {
//原文件路径
String path1 = "F:/1.txt";
//新文件路径
String path2 = "F:/2.txt";
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
String line = null;
try {
raf1 = new RandomAccessFile(path1, "r");
raf2 = new RandomAccessFile(path2, "rw");
while ((line = raf1.readLine()) != null) {
int start = line.indexOf("EMAIL");
int end = line.indexOf("OU=");
String a = line.substring(start, end);
String b = a.replace("ADDRESS", "");
line = line.replace(a, "");
line = b + line;
raf2.write(line.getBytes());
raf2.write('\n');
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
raf1.close();
raf2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
展开全部
这种功能用“查找-替换”不能实现吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class MyTest extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JButton jb1,jb2,jb3;
JTextArea jta;
public MyTest(){
this.setLayout(new FlowLayout());
jta = new JTextArea(10,24);
jb1 = new JButton("打开文件");
jb2 = new JButton("更新文件");
jb3 = new JButton("退出");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
this.add(jta);
this.add(jb1);
this.add(jb2);
this.add(jb3);
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("打开文件")){
JFileChooser chooser = new JFileChooser();
String oldStr = "CN=aa, EMAILADDRESS=bb, OU=cc, O=dd, C=ee\r\nCN=ff, EMAILADDRESS=gg, OU=hh, O=dd, C=ii";
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
//jta.setText(readStringFromFile(chooser.getSelectedFile()));
jta.setText(oldStr);
}
}
if(e.getActionCommand().equals("更新文件")){
JFileChooser chooser = new JFileChooser();
String newStr = "EMAIL=bb, CN=aa, OU=cc, O=dd, C=ee\r\nEMAIL=gg, CN=ff, OU=hh, O=dd, C=ii";
if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(this)) {
writeStringToFile(chooser.getSelectedFile(),newStr);
}
jta.setText(newStr);
}
if(e.getActionCommand().equals("退出")){
System.exit(0);
}
}
public static String readStringFromFile(File file) {
try {
FileReader fileReader = new FileReader(file);
int fileLength = (int) file.length();
char[] fileStr = new char[fileLength];
fileReader.read(fileStr);
fileReader.close();
StringBuffer sb = new StringBuffer();
sb.append(fileStr);
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void writeStringToFile(File file, String temp) {
writeStringToFile(file, temp, false);
}
public static void writeStringToFile(File file, String temp, boolean isAppend) {
try {
FileWriter fileWriter = new FileWriter(file, isAppend);
fileWriter.write(temp);
fileWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
new MyTest();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class MyTest extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JButton jb1,jb2,jb3;
JTextArea jta;
public MyTest(){
this.setLayout(new FlowLayout());
jta = new JTextArea(10,24);
jb1 = new JButton("打开文件");
jb2 = new JButton("更新文件");
jb3 = new JButton("退出");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
this.add(jta);
this.add(jb1);
this.add(jb2);
this.add(jb3);
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("打开文件")){
JFileChooser chooser = new JFileChooser();
String oldStr = "CN=aa, EMAILADDRESS=bb, OU=cc, O=dd, C=ee\r\nCN=ff, EMAILADDRESS=gg, OU=hh, O=dd, C=ii";
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
//jta.setText(readStringFromFile(chooser.getSelectedFile()));
jta.setText(oldStr);
}
}
if(e.getActionCommand().equals("更新文件")){
JFileChooser chooser = new JFileChooser();
String newStr = "EMAIL=bb, CN=aa, OU=cc, O=dd, C=ee\r\nEMAIL=gg, CN=ff, OU=hh, O=dd, C=ii";
if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(this)) {
writeStringToFile(chooser.getSelectedFile(),newStr);
}
jta.setText(newStr);
}
if(e.getActionCommand().equals("退出")){
System.exit(0);
}
}
public static String readStringFromFile(File file) {
try {
FileReader fileReader = new FileReader(file);
int fileLength = (int) file.length();
char[] fileStr = new char[fileLength];
fileReader.read(fileStr);
fileReader.close();
StringBuffer sb = new StringBuffer();
sb.append(fileStr);
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void writeStringToFile(File file, String temp) {
writeStringToFile(file, temp, false);
}
public static void writeStringToFile(File file, String temp, boolean isAppend) {
try {
FileWriter fileWriter = new FileWriter(file, isAppend);
fileWriter.write(temp);
fileWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
new MyTest();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class Main {
/**
* 测试方法
* @param args the command line arguments//未使用
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
String inputPath="D:\\1.txt";//输入的文本路径,注意\要转义
String outputPath="D:\\2.txt";//输出的文本路径
String pattern="EMAILADDRESS=\\w*, ";//要匹配的正则表达式,注意转义
String key="EMAILADDRESS";//要被替换的匹配文本
String replaced="EMAIL";//替换成的文本
List<String> lines= readAllLines(inputPath);//读取文件到LIST中
lines= replaceAll(lines, pattern, key, replaced);//执行自定义替换
writeAllLines(lines,outputPath);//写入LIST到文件中
}
static List<String> replaceAll(List<String> lines,String strPattern,String strKey,String strReplaceed)
{
List<String> rs=new LinkedList<String>();
Pattern pattern=Pattern.compile(strPattern);
for(String line:lines)
{
Matcher matcher=pattern.matcher(line);
matcher.find();
String partL=matcher.group().replaceAll(strKey,strReplaceed);
String partR=matcher.replaceFirst("");
String newLine=partL+partR;
rs.add(newLine);
}
return rs;
}
/**
* 写入所有行
* @param lines 内容
* @param path 要写入的路径
* @throws java.io.IOException
*/
static void writeAllLines(List<String> lines,String path) throws IOException
{
FileWriter fw=new FileWriter(path);
BufferedWriter bw=new BufferedWriter(fw);
for(String line:lines)
{
bw.append(line);
bw.newLine();
}
bw.close();
fw.close();
}
/**
* 读取所有行到LIST中
* @param path 读取文件路径
* @return 文件的所有文本行
* @throws java.io.FileNotFoundException
* @throws java.io.IOException
*/
static List<String> readAllLines(String path) throws FileNotFoundException, IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(path)));
List<String> lines=new LinkedList<String>();
String line;
while(( line=br.readLine())!=null)
{
lines.add(line);
}
br.close();
return lines;
}
}
import java.util.*;
import java.util.regex.*;
public class Main {
/**
* 测试方法
* @param args the command line arguments//未使用
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
String inputPath="D:\\1.txt";//输入的文本路径,注意\要转义
String outputPath="D:\\2.txt";//输出的文本路径
String pattern="EMAILADDRESS=\\w*, ";//要匹配的正则表达式,注意转义
String key="EMAILADDRESS";//要被替换的匹配文本
String replaced="EMAIL";//替换成的文本
List<String> lines= readAllLines(inputPath);//读取文件到LIST中
lines= replaceAll(lines, pattern, key, replaced);//执行自定义替换
writeAllLines(lines,outputPath);//写入LIST到文件中
}
static List<String> replaceAll(List<String> lines,String strPattern,String strKey,String strReplaceed)
{
List<String> rs=new LinkedList<String>();
Pattern pattern=Pattern.compile(strPattern);
for(String line:lines)
{
Matcher matcher=pattern.matcher(line);
matcher.find();
String partL=matcher.group().replaceAll(strKey,strReplaceed);
String partR=matcher.replaceFirst("");
String newLine=partL+partR;
rs.add(newLine);
}
return rs;
}
/**
* 写入所有行
* @param lines 内容
* @param path 要写入的路径
* @throws java.io.IOException
*/
static void writeAllLines(List<String> lines,String path) throws IOException
{
FileWriter fw=new FileWriter(path);
BufferedWriter bw=new BufferedWriter(fw);
for(String line:lines)
{
bw.append(line);
bw.newLine();
}
bw.close();
fw.close();
}
/**
* 读取所有行到LIST中
* @param path 读取文件路径
* @return 文件的所有文本行
* @throws java.io.FileNotFoundException
* @throws java.io.IOException
*/
static List<String> readAllLines(String path) throws FileNotFoundException, IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(path)));
List<String> lines=new LinkedList<String>();
String line;
while(( line=br.readLine())!=null)
{
lines.add(line);
}
br.close();
return lines;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void main(String[] args) throws IOException {
List file = FileUtils.readLines(new File("c:/test.txt"));
String[] s = new String[2];
List r = new ArrayList();
for (int i = 0; i < file.size(); i++) {
s = file.get(i).toString().split(",");
String temp = s[1].trim(); // 获取EMAILADDRESS
s[1] = s[0].trim();
s[0] = temp; // 颠倒位置
r.add(Arrays.toString(s).replaceAll(" ", " ").replaceAll("EMAILADDRESS", "EMAIL")); // 替换
}
FileUtils.writeLines(new File("c:/finalText.txt"), r);
}
注:import org.apache.commons.io.FileUtils;
懒得写java读写了,正好apache有现成的,拿来用了就
List file = FileUtils.readLines(new File("c:/test.txt"));
String[] s = new String[2];
List r = new ArrayList();
for (int i = 0; i < file.size(); i++) {
s = file.get(i).toString().split(",");
String temp = s[1].trim(); // 获取EMAILADDRESS
s[1] = s[0].trim();
s[0] = temp; // 颠倒位置
r.add(Arrays.toString(s).replaceAll(" ", " ").replaceAll("EMAILADDRESS", "EMAIL")); // 替换
}
FileUtils.writeLines(new File("c:/finalText.txt"), r);
}
注:import org.apache.commons.io.FileUtils;
懒得写java读写了,正好apache有现成的,拿来用了就
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询