html怎么让用户选择文件,读取多个文本文件,附加一些html标签,然后将其保存到用户在java中选择的目录...

我希望用户可以选择输入和输出目录。

以下是我迄今尝试过的代码:

public class FileOpen extends JPanel implements ActionListener {

private static final long serialVersionUID = 1L;

static private final String newline = "\n";

JButton openButton, convertButton;

JTextArea log;

JFileChooser fc;

public FileOpen() {

super(new BorderLayout());

log = new JTextArea(5, 20);

log.setMargin(new Insets(5, 5, 5, 5));

log.setEditable(false);

JScrollPane logScrollPane = new JScrollPane(log);

// Create a file chooser

fc = new JFileChooser();

openButton = new JButton("Open a Text File...");

openButton.addActionListener(this);

convertButton = new JButton("Convert to HTML File...");

convertButton.addActionListener(this);

// For layout purposes, put the buttons in a separate panel

JPanel buttonPanel = new JPanel(); // use FlowLayout

buttonPanel.add(openButton);

buttonPanel.add(convertButton);

// Add the buttons and the log to this panel.

add(buttonPanel, BorderLayout.PAGE_START);

add(logScrollPane, BorderLayout.CENTER);

}

public void actionPerformed(ActionEvent e) {

// Handle open button action.

if (e.getSource().equals(openButton)) {

fc.setMultiSelectionEnabled(true);

FileNameExtensionFilter filter = new FileNameExtensionFilter(

"Only Text Files", "txt");

fc.setFileFilter(filter);

int returnVal = fc.showOpenDialog(FileOpen.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {

File files[] = null;

files = fc.getSelectedFiles();

for (int i = 0; i <= files.length - 1; i++) {

log.append("Opening " + files[i].getName() + "\n ");

try {

FileReader fr = new FileReader(files[i]);

BufferedReader br = new BufferedReader(fr);

String strLine;

StringBuilder fileContent = new StringBuilder();

// Read File Line By Line

log.append("Reading files " + files[i].getName()

+ "\n ");

while ((strLine = br.readLine()) != null) {

String tokens[] = strLine.split(" ");

if (tokens.length > 0) {

fileContent.append(strLine);

fileContent.append("
");

FileWriter fstreamWrite = new FileWriter(

files[i]);

fstreamWrite.write("");

fstreamWrite.write("\n");

fstreamWrite.write("

");

fstreamWrite.write("\n");

fstreamWrite.write("

");

fstreamWrite.write("\n");

BufferedWriter out = new BufferedWriter(

fstreamWrite);

RandomAccessFile raf = new RandomAccessFile(

files[i], "rw");

raf.seek(0);

out.write(fileContent.toString());

out.newLine();

out.write("");

out.newLine();

out.write("");

out.newLine();

System.out.println(fileContent);

raf.close();

out.flush();

out.close();

br.close();

}

}

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

} else {

log.append("Operation Canceled \n");

}

// Handle Covert button action.

} else if (e.getSource() == convertButton) {

}

}

@SuppressWarnings("rawtypes")

private static void createAndShowGUI() {

// Create and set up the window.

JFrame frame = new JFrame("TextToHtml");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Add content to the window.

frame.add(new FileOpen());

// Display the window.

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

// Turn off metal's use of bold fonts

UIManager.put("swing.boldMetal", Boolean.FALSE);

createAndShowGUI();

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值