文本分类java源代码 java文本分析程序

java记事本源代码给你个做好了的Java的源程序的记事本 , 自己看看就行了的,不怎么难的···
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class MyNotepad implements ActionListener{
private JFrame frame=new JFrame("新记事本");
private JTextArea jta=new JTextArea();
private String result="";
private boolean flag=true;
private File f;
private JButton jb=new JButton("开始");
private JTextField jtf=new JTextField(15);
private JTextField jt=new JTextField(15);
private JButton jbt=new JButton("替换为");
private JButton jba=new JButton("全部替换");
private Icon ic=new ImageIcon("D:\\java课堂笔记\\GUI\\11.gif");
private String value;
private int start=0;
private JFrame jf=new JFrame("查找");
private JFrame jfc=new JFrame("替换");
@Override
public void actionPerformed(ActionEvent e) {
String comm=e.getActionCommand();
if("新建".equals(comm)){
if(!(frame.getTitle().equals("新记事本"))){
if(!flag){
write();
newNew();
}else{
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句 , 保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
}
}
}else if(!(jta.getText().isEmpty())){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
newNew();
}
}else{
newNew();
}
}else if("打开".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
jfc.setDialogType(JFileChooser.OPEN_DIALOG);
int returnVal = jfc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
frame.setTitle(f.getName());
result=read();
flag=false;
value=https://www.04ip.com/post/result;
jta.setText(result);
}
}else if("保存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
if(flag){
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
}
}else{
write();
}
}else if("另存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"另存");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
write();
}
}else if("退出".equals(comm)){
System.exit(0);
}else if("撤销".equals(comm)){
jta.setText(value);
}else if("剪切".equals(comm)){
value=https://www.04ip.com/post/jta.getText();
jta.cut();
}else if("复制".equals(comm)){
jta.copy();
}else if("粘贴".equals(comm)){
value=https://www.04ip.com/post/jta.getText();
jta.paste();
}else if("删除".equals(comm)){
value=https://www.04ip.com/post/jta.getText();
jta.replaceSelection(null);
}else if("全选".equals(comm)){
jta.selectAll();
}else if("查找".equals(comm)){
value=https://www.04ip.com/post/jta.getText();
jf.add(jtf,BorderLayout.CENTER);
jf.add(jb,BorderLayout.SOUTH);
jf.setLocation(300,300);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("替换".equals(comm)){
value=https://www.04ip.com/post/jta.getText();
GridLayout gl=new GridLayout(3,3);
JLabel jl1=new JLabel("查找内容:");
JLabel jl2=new JLabel("替换为:");
jfc.setLayout(gl);
jfc.add(jl1);
jfc.add(jtf);
jfc.add(jb);
jfc.add(jl2);
jfc.add(jt);
jfc.add(jbt);
JLabel jl3=new JLabel();
JLabel jl4=new JLabel();
jfc.add(jl3);
jfc.add(jl4);
jfc.add(jba);
jfc.setLocation(300,300);
jfc.pack();
jfc.setVisible(true);
jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("版本".equals(comm)){
JDialog jd=new JDialog(frame,"关于对话框");
jd.setSize(200,200);
JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");
jd.add(l,BorderLayout.CENTER);
jd.setLocation(100,200);
jd.setSize(300,300);
jd.setVisible(true);
//jd.pack();
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}else if("开始".equals(comm)||"下一个".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s 1;
jb.setText("下一个");
//value=https://www.04ip.com/post/value.substring(s temp.length());//不能截取字串
}else {
JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);
jf.dispose();
}
}else if("替换为".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s 1;
jta.replaceSelection(jt.getText());
}else {
JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);
jf.dispose();
}
}else if("全部替换".equals(comm)){
String temp=jta.getText();
temp=temp.replaceAll(jtf.getText(), jt.getText());
jta.setText(temp);
}
}
public String read(){
String temp="";
try {
FileInputStream fis = new FileInputStream(f.getAbsolutePath());
byte[] b=new byte[1024];
while(true){
int num=fis.read(b);
if(num==-1)break;
temp=temp new String(b,0,num);
}
fis.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return temp;
}
public void write(){
try {
FileOutputStream fos=new FileOutputStream(f);
fos.write(jta.getText().getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void newNew(){
frame.dispose();
new MyNotepad();
flag=true;
}
public MyNotepad(){
JMenuBar jmb=new JMenuBar();
String[] menuLab={"文件","编辑","帮助"};
String[][] menuItemLab={{"新建","打开","保存","另存","退出"},
{"撤销","剪切","复制","粘贴","删除","全选","查找","替换"},
{"版本"}};
for(int i=0;imenuLab.length;i){
JMenu menu=new JMenu(menuLab[i]);
jmb.add(menu);
for(int j=0;jmenuItemLab[i].length;j){
JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);
menu.add(jmi);
jmi.addActionListener(this);
}
}
frame.setJMenuBar(jmb);
jta.setLineWrap(true);//自动换行
JScrollPane jsp=new JScrollPane(jta);//滚动窗口面板
frame.add(jsp);
jb.addActionListener(this);
jbt.addActionListener(this);
jba.addActionListener(this);
frame.setLocation(200,50);
frame.setSize(620,660);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyNotepad();
}
}
如何用JAVA实现根据文件后缀名分类文件,并且将文件复制到不同的文件夹处理的代码逻辑如下:
public static void main(String args[]) {
String saveToFileDir = "F:\\整理后的文件存放目录";
File file = new File("F:\\所需整理的文件目录");
processFileFenLei(saveToFileDir, file);
}
private static void processFileFenLei(String saveToFileDir, File file) {
if (file.getName().startsWith(".")) {
return;
}
System.out.println("当前处理位置==="file.getAbsolutePath());
if (file.isFile()) {
processCopyFile(saveToFileDir, file);
} else {
File[] subFiles = file.listFiles();
for (File subFile : subFiles) {
processFileFenLei(saveToFileDir, subFile);
}
}
}
private static void processCopyFile(String saveToFileDir, File file) {
String extFileName = FileCreateUtil.getFileExtension(file);
String wholeDir = saveToFileDir"\\"extFileName;
File fileDir = new File(wholeDir);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
File saveToFile = new File(wholeDir"\\"file.getName());
FileCreateUtil.saveFileToFile(file, saveToFile);
}
以上代码中所用到的文件操作工具类:
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.InputStreamReader;
import java.sql.Blob;
import java.sql.SQLException;
/**
* @作者 王建明
* @创建日期 Feb 4, 2010
* @创建时间 9:56:15 AM
* @版本号 V 1.0
*/
public class FileCreateUtil {
private static final String CONTENT_TYPE_IMAGE = "image/jpeg";
/**
* @说明 将二进制字节流保存为文件
* @param byteArry二进制流
* @param file要保存的文件
* @throws SQLException
* @throws IOException
*/
public static void saveByteArry2File(byte[] byteArry, File file)
throws SQLException, IOException {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
bos.write(byteArry);
bos.close();
}
/**
* @说明 将文件转换为二进制字节流
* @param file要转换的文件
* @return
* @throws SQLException
* @throws IOException
*/
public static byte[] changeFile2Bytes(File file) throws SQLException,
IOException {
long len = file.length();
byte[] bytes = new byte[(int) len];
FileInputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(
inputStream);
int r = bufferedInputStream.read(bytes);
if (r != len) {
throw new IOException("File read error");
}
inputStream.close();
bufferedInputStream.close();
return bytes;
}
/**
* @说明 将Blob类类型的文件转换成二进制字节流
* @param pic
* @return
* @throws SQLException
* @throws IOException
*/
public static byte[] changeBlob2Bytes(Blob pic) throws SQLException,
IOException {
byte[] bytes = pic.getBytes(1, (int) pic.length());
return bytes;
}
/**
* @说明 将Blob类类型的数据保存为本地文件
* @param blob
* @param file
* @throws SQLException
* @throws IOException
*/
public static void saveBlob2File(Blob blob, File file) throws SQLException,
IOException {
InputStream is = blob.getBinaryStream();
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
int b = -1;
while ((b = is.read()) != -1) {
bos.write(b);
}
bos.close();
is.close();
}
/**
* @说明 将一个文件拷贝到另一个文件中
* @param oldFile源文件
* @param newFile目标文件
*/
public static void saveFileToFile(File oldFile, File newFile) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(oldFile); // 建立文件输入流
fos = new FileOutputStream(newFile);
int r;
while ((r = fis.read()) != -1) {
fos.write((byte) r);
}
} catch (FileNotFoundException ex) {
System.out.println("Source File not found");
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
try {
if (fis != null)
fis.close();
if (fos != null)
fos.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
}
/**
* @说明 获取文本形式文件的内容
* @param file要读取的文件
* @return
*/
public static String getTxtFileContent(File file) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line = null;
StringBuilder sb = new StringBuilder((int) file.length());
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
return sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("File not found");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Read file error");
} finally {
try {
if (br != null)
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "";
}
/**
* @说明 把一个文件转化为字节
* @param file
* @return byte[]
* @throws Exception
*/
public static byte[] getByteFromFile(File file) throws Exception {
byte[] bytes = null;
if (file != null) {
InputStream is = new FileInputStream(file);
int length = (int) file.length();
if (lengthInteger.MAX_VALUE) // 当文件的长度超过了int的最大值
{
System.out.println("this file is max ");
return null;
}
bytes = new byte[length];
int offset = 0;
int numRead = 0;
while (offsetbytes.length
(numRead = is.read(bytes, offset, bytes.length - offset)) = 0) {
offset= numRead;
}
// 如果得到的字节长度和file实际的长度不一致就可能出错了
if (offsetbytes.length) {
System.out.println("file length is error");
return null;
}
is.close();
}
return bytes;
}
/**
* @说明 将指定文本内容写入到指定文件中(原文件将被覆盖!)
* @param content要写入的内容
* @param file写到的文件
*/
public static void writeContentIntoFile(String content, File file) {
try {
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
fw.close();
} catch (IOException e) {
System.out.println("Write file error");
e.printStackTrace();
}
}
/**
* @param file
* @param encode
* @return
* @throws Exception
*T:2012-03-01 11:12:51 A:王建明 X:问题ID—— R:备注——读取文件时设置txt文件的编码方式
*/
public static String readFileContent(File file, String encode)
throws Exception {
StringBuilder sb = new StringBuilder();
if (file.isFile()file.exists()) {
InputStreamReader insReader = new InputStreamReader(
new FileInputStream(file), encode);
BufferedReader bufReader = new BufferedReader(insReader);
String line = new String();
while ((line = bufReader.readLine()) != null) {
// System.out.println(line);
sb.append(line"\n");
}
bufReader.close();
insReader.close();
}
return sb.toString();
}
/**
* @param file
* @return T:2012-03-01 11:12:25 A:王建明 X:问题ID—— R:备注——获取txt文件的编码格式
*/
public static String getTxtEncode(File file) {
String code = "";
try {
InputStream inputStream = new FileInputStream(file);
byte[] head = new byte[3];
inputStream.read(head);
code = "gb2312";
if (head[0] == -1head[1] == -2)
code = "UTF-16";
if (head[0] == -2head[1] == -1)
code = "Unicode";
if ((head[0] == -17head[1] == -69head[2] == -65))
code = "UTF-8";
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return code;
}
/**
* @说明 获取文件后缀名
* @param file
* @return
*/
public static String getFileExtension(File file) {
if (file != nullfile.isFile()) {
String filename = file.getName();
int i = filename.lastIndexOf(".");
if (i0ifilename.length() - 1) {
return filename.substring(i1).toLowerCase();
}
}
return "";
}
/**
* @说明 删除文件或文件夹
* @param file
* @作者 王建明
* @创建日期 2012-5-26
* @创建时间 上午09:36:58
* @描述 ——
*/
public static void deleteFile(File file) {
if (file.exists()) { // 判断文件是否存在
if (file.isFile()) { // 判断是否是文件
file.delete(); // delete()方法 你应该知道 是删除的意思;
} else if (file.isDirectory()) { // 否则如果它是一个目录
File files[] = file.listFiles(); // 声明目录下所有的文件 files[];
for (int i = 0; ifiles.length; i) { // 遍历目录下所有的文件
deleteFile(files[i]); // 把每个文件 用这个方法进行迭代
}
}
file.delete();
} else {
System.out.println("所删除的文件不存在!"'\n');
}
}
/**
* @说明 创建文件夹,如果不存在的话
* @param dirPath
* @作者 王建明
* @创建日期 2012-5-26
* @创建时间 上午09:49:12
* @描述 ——
*/
public static void createMirs(String dirPath) {
File dirPathFile = new File(dirPath);
if (!dirPathFile.exists())
dirPathFile.mkdirs();
}
}
有没有好用的java编写的分类器,将一些文本文件进行分类?文本分类文本分类java源代码的重点不在分类文本分类java源代码,而在怎么样描述文本模型和提取文档中的terms并把它数字化,转化为分类器可以使用的输入类型 。这前面的处理直接关系到后面分分类效果 。就文本模型而言,现在普遍使用的还是Salton 和 McGill的Vector Space Model, 通过TF-IDF统计,如果是英文的话,要进行语义层次的抽象, 这方面可以使用WordNet,网上可以找到JAVA wordnet 的API,,个人认为比较好的是JWNL 如果是中文的话,涉及到分词,中科院计算所分词系统ICTCLAS,可到其网站上下载免费版 。
至于JAVA写的分类器很多,常用的比如说Weka,RapidMiner(这个相当不错,有专门的Web data Mining的扩展包 , 是我的最爱),这些都可以自己调用其接口实现新的算法 。个人强烈推荐使用RapidMiner,功能相当强大,几乎实现文本分类java源代码了当前的所有机器学习的算法,并且操纵简便 。
学习提示:不要刚开始就希望效果很好,急躁是做学问的大敌 , 肯定会出现一些意想不到的问题,最主要是勤于思考,善于查找问题,慢慢改进,一个问题,如果比较难 , 回到它最原始最简单的问题上去 。祝你学业进步 。
Java100行以上源代码,至少五个class以及一个interface,可以简单点?下面是一个可能文本分类java源代码的Java源代码文本分类java源代码,它包含文本分类java源代码了一个接口(Shape)和五个类(Circle, Rectangle, Triangle, Square 和 Main) 。它的功能是计算不同形状的面积和周长 。
//定义一个接口Shape文本分类java源代码,有两个抽象方法文本分类java源代码:getArea()和getPerimeter()interface Shape {double getArea();double getPerimeter();
}//定义一个类Circle,实现Shape接口class Circle implements Shape {//定义一个私有属性radius,表示圆的半径
private double radius;//定义一个公有构造方法 , 用于初始化radius
public Circle(double radius) {this.radius = radius;
}//实现getArea()方法,返回圆的面积
public double getArea() {return Math.PI * radius * radius;
}//实现getPerimeter()方法,返回圆的周长
public double getPerimeter() {return Math.PI * radius * 2;
}
}//定义一个类Rectangle , 实现Shape接口class Rectangle implements Shape {//定义两个私有属性width和height,表示矩形的宽度和高度
private double width;private double height;//定义一个公有构造方法,用于初始化width和height
public Rectangle(double width, double height) {this.width = width;this.height = height;
}//实现getArea()方法,返回矩形的面积
public double getArea() {return width * height;
}//实现getPerimeter()方法,返回矩形的周长
public double getPerimeter() {return (widthheight) *2;
}
}//定义一个类Triangle,实现Shape接口class Triangle implements Shape {//定义三个私有属性a,b,c表示三角形的三条边长
private double a;private double b;private double c;//定义一个公有构造方法 , 用于初始化a,b,c,并检查是否满足三角形条件(任意两边之和大于第三边)
public Triangle(double a, double b, double c) throws Exception{if (abcacbbca) {
this.a = a;this.b = b;
this.c = c;
} else {
throw new Exception("Invalid triangle");
}
}//实现getArea()方法,返回三角形的面积(使用海伦公式)
public double getArea() {//计算半周长p
double p = (abc) /2;//计算并返回面积s(使用Math.sqrt()函数求平方根)
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}//实现getPerimeter()方法,返回三角形的周长
public double getPerimeter(){return abc;
}
}//定义一个类Square,继承Rectangle类 , 并重写构造方法和toString()方法class Square extends Rectangle {//重写构造方法,在调用父类构造方法时传入相同的参数side作为width和height
public Square(double side){super(side, side);
}//重写toString()方法 , 在原来基础上加上"Square:"前缀,并只显示side属性而不显示width和height属性(使用String.format()函数格式化字符串)
@Override
public String toString(){return String.format("Square: side=%.2f", super.width);/* 或者直接使用super.getPerimeter()/4作为side */
/* return String.format("Square: side=%.2f", super.getPerimeter()/4); */
/* 注意:不能直接访问super.side属性 ,
java读取txt文件并且进行分类public static void main(String[] args) throws Exception{
File file=new File("D:\\output.txt");
BufferedReader reader=new BufferedReader(new FileReader(file));
String r=null;
int one=0,two=0,three=0;
String [] temp=null;
while((r=reader.readLine())!=null)
{
if(r.contains("one"))
{
temp=r.split(":", r.length());
if(temp!=null)
{
if(!" ".equals(temp[1]))
{
one =Integer.valueOf(temp[1]);
}
else{
one =0;
}
}
}
else if(r.contains("two"))
{
temp=r.split(":", r.length());
if(temp!=null)
{
if(!" ".equals(temp[1]))
{
two =Integer.valueOf(temp[1]);
}
else{
two =0;
}
}
}
else if(r.contains("three"))
{
temp=r.split(":", r.length());
if(temp!=null)
{
if(!"".equals(temp[1]))
{
three =Integer.valueOf(temp[1]);
}
else{
three =0;
}
}
}
}
System.out.println("One=" one " ;Two=" two " ;Three=" three);
}
自己测试一下:这是文本分类java源代码我文本分类java源代码的输出结果
Console:
One=190 ;Two=0 ;Three=410
求一个用JAVA写的简单的记事本源代码程序import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;
class MyMenuBar extends MenuBar{
public MyMenuBar(Frame parent){
parent.setMenuBar(this);
}
public void addMenus(String [] menus){
for(int i=0;imenus.length;i)
add(new Menu(menus[i]));
}
public void addMenuItems(int menuNumber,String[] items){
for(int i=0;iitems.length;i){
if(items[i]!=null)
getMenu(menuNumber).add(new MenuItem(items[i]));
else getMenu(menuNumber).addSeparator();
}
}
public void addActionListener(ActionListener al){
for(int i=0;igetMenuCount();i)
for(int j=0;jgetMenu(i).getItemCount();j)
getMenu(i).getItem(j).addActionListener(al);
}
}
class MyFile{
private FileDialog fDlg;
public MyFile(Frame parent){
fDlg=new FileDialog(parent,"",FileDialog.LOAD);
}
private String getPath(){
return fDlg.getDirectory() "\\" fDlg.getFile();
}
public String getData() throws IOException{
fDlg.setTitle("打开");
fDlg.setMode(FileDialog.LOAD);
fDlg.setVisible(true);
BufferedReader br=new BufferedReader(new FileReader(getPath()));
StringBuffer sb=new StringBuffer();
String aline;
while((aline=br.readLine())!=null)
sb.append(aline '\n');
br.close();
return sb.toString();
}
public void setData(String data) throws IOException{
fDlg.setTitle("保存");
fDlg.setMode(FileDialog.SAVE);
fDlg.setVisible(true);
BufferedWriter bw=new BufferedWriter(new FileWriter(getPath()));
bw.write(data);
bw.close();
}
}
class MyClipboard{
private Clipboard cb;
public MyClipboard(){
cb=Toolkit.getDefaultToolkit().getSystemClipboard();
}
public void setData(String data){
cb.setContents(new StringSelection(data),null);
}
public String getData(){
Transferable content=cb.getContents(null);
try{
return (String) content.getTransferData(DataFlavor.stringFlavor);
//DataFlavor.stringFlavor会将剪贴板中的字符串转换成Unicode码形式的String对象 。
//DataFlavor类是与存储在剪贴板上的数据的形式有关的类 。
}catch(Exception ue){}
return null;
}
}
class MyFindDialog extends Dialog implements ActionListener{
private Label lFind=new Label("查找字符串");
private Label lReplace=new Label("替换字符串");
private TextField tFind=new TextField(10);
private TextField tReplace=new TextField(10);
private Button bFind=new Button("查找");
private Button bReplace=new Button("替换");
private TextArea ta;
public MyFindDialog(Frame owner,TextArea ta){
super(owner,"查找",false);
this.ta=ta;
setLayout(null);
lFind.setBounds(10,30,80,20);
lReplace.setBounds(10,70,80,20);
tFind.setBounds(90,30,90,20);
tReplace.setBounds(90,70,90,20);
bFind.setBounds(190,30,80,20);
bReplace.setBounds(190,70,80,20);
add(lFind);
add(tFind);
add(bFind);
add(lReplace);
add(tReplace);
add(bReplace);
setResizable(false);
bFind.addActionListener(this);
bReplace.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyFindDialog.this.dispose();
}
});
}//构造函数结束
public void showFind(){
setTitle("查找");
setSize(280,60);
setVisible(true);
}
public void showReplace(){
setTitle("查找替换");
setSize(280,110);
setVisible(true);
}
private void find(){
String text=ta.getText();
String str=tFind.getText();
int end=text.length();
int len=str.length();
int start=ta.getSelectionEnd();
if(start==end) start=0;
for(;start=end-len;start){
if(text.substring(start,start len).equals(str)){
ta.setSelectionStart(start);
ta.setSelectionEnd(start len);
return;
}
}
//若找不到待查字符串,则将光标置于末尾
ta.setSelectionStart(end);
ta.setSelectionEnd(end);
}
public Button getBFind() {
return bFind;
}
private void replace(){
String str=tReplace.getText();
if(ta.getSelectedText().equals(tFind.getText()))
ta.replaceRange(str,ta.getSelectionStart(),ta.getSelectionEnd());
else find();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bFind)
find();
else if(e.getSource()==bReplace)
replace();
}
}
public class MyMemo extends Frame implements ActionListener{
private TextArea editor=new TextArea(); //可编辑的TextArea
private MyFile mf=new MyFile(this);//MyFile对象
private MyClipboard cb=new MyClipboard();
private MyFindDialog findDlg=new MyFindDialog(this,editor);
public MyMemo(String title){//构造函数
super(title);
MyMenuBar mb=new MyMenuBar(this);
//添加需要的菜单及菜单项
mb.addMenus(new String[]{"文件","编辑","查找","帮助"});
mb.addMenuItems(0,new String[]{"新建","打开","保存",null,"全选"});
mb.addMenuItems(1,new String[]{"剪贴","复制","粘贴","清除",null,"全选"});
mb.addMenuItems(2,new String[]{"查找",null,"查找替换"});
mb.addMenuItems(3,new String[]{"我的记事本信息"});
add(editor);//为菜单项注册动作时间监听器
mb.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyMemo.this.dispose();
}
});//分号不能忘了
}//构造函数完
public void actionPerformed(ActionEvent e){
String selected=e.getActionCommand(); //获取菜单项标题
if(selected.equals("新建"))
editor.setText("");
else if(selected.equals("打开")){
try{
editor.setText(mf.getData());
}catch(IOException ie){}
}
else if(selected.equals("保存")){
try{
mf.setData(editor.getText());
}catch(IOException ie){}
}
else if(selected.equals("退出")){
dispose();
}
else if(selected.equals("剪贴")){
//将选中的字符串复制到剪贴板中并清除字符串
cb.setData(editor.getSelectedText());
editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());
}
else if(selected.equals("复制")){
cb.setData(editor.getSelectedText());
}
else if(selected.equals("粘贴")){
String str=cb.getData();
editor.replaceRange(str,editor.getSelectionStart(),editor.getSelectionEnd());
//粘贴在光标位置
}
else if(selected.equals("清除")){
editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());
}
else if(selected.equals("全选")){
editor.setSelectionStart(0);
editor.setSelectionEnd(editor.getText().length());
}
else if(selected.equals("查找")){
findDlg.showFind();
}
else if(selected.equals("查找替换")){
findDlg.showReplace();
}
}
public static void main(String[] args){
MyMemo memo=new MyMemo("记事本");
memo.setSize(650,450);
memo.setVisible(true);
}
}
【文本分类java源代码 java文本分析程序】关于文本分类java源代码和java文本分析程序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读