文件内再分类到各txt文件

2022-11-29,,

当老师叫我们帮他做事,比如文件内内容再分类,我们就可以建个面板,里面有各要导入文件按钮,先把分类内容copy下,再点按钮导入进txt文件就行啦。

以下为java代码,使用了tableLayout布局

package classification;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import layout.TableLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable; public class Classification extends JFrame implements ActionListener{
public Classification(){ // 创建一个窗口。
Frame frame = new Frame("Classification");
frame.setBounds (1300 , 100, 200, 450); //设置背景颜色。
frame.setBackground(Color.black); //在这个窗口创建一个网格。
double size[][] =
{
//0 ,1 ,2 ,3
{TableLayout.FILL,TableLayout.FILL}, //列
{TableLayout.FILL,TableLayout.FILL,TableLayout.FILL,TableLayout.FILL,TableLayout.FILL} //行
};
TableLayout layout = new TableLayout(size);
frame.setLayout(layout); //创建一些按钮. //编辑字体
Font B = new Font("宋体",Font.PLAIN,30); //写入文本数组。
for (int i = 0;i < Button.length;i++)
{
button[i] = new Button(Button[i]);
} //应用字体。
//按钮字体。
for(int i = 0; i <= 9; i++) {
button[i].setFont(B);
button[i].setForeground(Color.black);
} //添加按钮
frame.add(button[0],"0,0,0,0"); //1
frame.add(button[1],"1,0,1,0"); //2
frame.add(button[2],"0,1,0,1"); //3
frame.add(button[3],"1,1,1,1"); //4
frame.add(button[4],"0,2,0,2"); //5
frame.add(button[5],"1,2,1,2"); //6
frame.add(button[6],"0,3,0,3"); //7
frame.add(button[7],"1,3,1,3"); //8
frame.add(button[8],"0,4,0,4"); //9
frame.add(button[9],"1,4,1,4"); //撤回 for(int i = 0; i <= 9; i++) {
button[i].addActionListener(this) ;
} //编辑按钮
for(int i = 0; i <= 9; i++) {
button[i].setFont(B);
button[i].setBackground(Color.green);
} //添加窗口关闭。
frame.addWindowListener
(new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
); //窗口置顶。
frame.setAlwaysOnTop(true); //显示窗口。
frame.show(); }
String Button[] = {"1","2","3","4","5","6","7","8","9",""};
Button button[] = new Button[Button.length]; public static void main(String[] args) {
new Classification();
} //路径
String[] s = {
"D:\\homework\\chapter1.txt",
"D:\\homework\\chapter2.txt",
"D:\\homework\\chapter3.txt",
"D:\\homework\\chapter4.txt",
"D:\\homework\\chapter5.txt",
"D:\\homework\\chapter6.txt",
"D:\\homework\\chapter7.txt",
"D:\\homework\\chapter8.txt",
"D:\\homework\\chapter9.txt"
}; //文件读入粘贴板内容
public static void method2(String fileName, String content) {
FileWriter writer = null;
try {
writer = new FileWriter(fileName,true);
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
// 获取剪切板中的内容
Transferable clipTf = sysClip.getContents(null);
if (clipTf != null) {
// 检查内容是否是文本类型
if (clipTf.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
content = (String) clipTf
.getTransferData(DataFlavor.stringFlavor);
writer.write(content);
} catch (Exception e) {
e.printStackTrace();
}
}
} }catch(IOException e) {
e.printStackTrace();
}
finally {
try {
if(writer != null) {
writer.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
//按钮响应
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button[0])
try {
method2(s[0],"");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(e.getSource() == button[1])
try {
method2(s[1],"");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(e.getSource() == button[2])
try {
method2(s[2], "");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(e.getSource() == button[3])
try {
method2(s[3],"");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(e.getSource() == button[4])
try {
method2(s[4],"");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(e.getSource() == button[5])
try {
method2(s[5],"");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(e.getSource() == button[6])
try {
method2(s[6],"");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(e.getSource() == button[7])
try {
method2(s[7],"");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(e.getSource() == button[8])
try {
method2(s[8],"");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} }

 
 

文件内再分类到各txt文件的相关教程结束。

《文件内再分类到各txt文件.doc》

下载本文的Word格式文档,以方便收藏与打印。