本文目录一览:
- 1、java 如何在一篇文章中取出某个关键字
- 2、java编程 求一个能够从TXT文本中提取中文关键字并输出的程序
- 3、如何用java实现文本的关键字提取
- 4、java word中提取关键字
- 5、求一个提取文章关键词的java程序
java 如何在一篇文章中取出某个关键字
这个方法是取出了含有keyword的那一行,你要去关键字,只要返回true,直接取就O了!!忘楼主采纳!
public void selectKeyWord(String keyWord){
String filepath = "";
try {
DataInputStream dis = new DataInputStream(new FileInputStream(filepath));
while(dis.read()!=-1){
String str = dis.readUTF();
if(str.contains(keyWord)){
System.out.println(str);
}
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
java编程 求一个能够从TXT文本中提取中文关键字并输出的程序
以下这个程序就是 获取到.txt中的中文,然后在控制台中输出
希望这个能帮到你啊,望采纳啊
package cn.ima_21;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args) throws IOException {
// 创建对象
FileReader fr = new FileReader("a.txt");
//开发一般如下写
int num = 0;
while((num=fr.read())!=-1){
System.out.print((char)num);
}
//释放资源
fr.close();
}
}
如何用java实现文本的关键字提取
可以使用java API中的URL抓取流,也可以使用Apache的HttpClient等多种方法,最终得到的就是字符串咯,得到字符串就好办了,使用正则匹配,将匹配的保存起来就可以了
java word中提取关键字
首先,要用Java的File类中函数(如open,read等,记不清了)打开word文档,利用形式语言与自动机的原理,跳过word文档的头部,分析内容,利用String中的equals()方法匹配"java"字符串。
求一个提取文章关键词的java程序
//直接粘贴就行。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.EtchedBorder;
import javax.swing.filechooser.FileFilter;
public class Application2 extends JFrame implements Cloneable{
public Application2(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(800,700);
this.setLayout(new BorderLayout());
keyWords1=new String[]{"那么","还是","sdf"};
keyWords2=new String[]{"所以","而且",};
input=new JTextArea();
JPanel ip=new JPanel();
ip.setLayout(new BorderLayout());
ip.add(input,BorderLayout.CENTER);
ip.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "输入文本"));
output1=new JTextArea();
JPanel o1p=new JPanel();
o1p.setLayout(new BorderLayout());
o1p.add(output1,BorderLayout.CENTER);
o1p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "以下为"));
output2=new JTextArea();
JPanel o2p=new JPanel();
o2p.setLayout(new BorderLayout());
o2p.add(output2,BorderLayout.CENTER);
o2p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "以下为"));
JSplitPane split1=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,o1p,o2p);
split1.setDividerLocation(350);
JSplitPane split2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,ip,split1);
split2.setDividerLocation(300);
this.add(split2,BorderLayout.CENTER);
open=new JButton("导入");
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser chooser=new JFileChooser(".");
chooser.setMultiSelectionEnabled(false);
chooser.addChoosableFileFilter(new FileFilter(){
@Override
public boolean accept(File file) {
if(file.isDirectory())
return true;
int length=file.getName().length();
if(length5)
return false;
if(file.getName().substring(length-4).equals(".txt"))
return true;
return false;
}
@Override
public String getDescription() {
return "文本文件";
}
});
chooser.showOpenDialog(Application2.this);
File file=chooser.getSelectedFile();
if(file==null)
return;
try {
Scanner sc=new Scanner(file);
String text="";
while(sc.hasNextLine())
text+=sc.nextLine()+"\n";
input.setText(text);
String[] array=getSentences();
output1.setText(getKeySentences(keyWords1,array));
output2.setText(getKeySentences(keyWords2,array));
}catch (IOException e1) {
e1.printStackTrace();
}
}
});
save=new JButton("导出");
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser chooser=new JFileChooser(".");
chooser.setMultiSelectionEnabled(false);
chooser.addChoosableFileFilter(new FileFilter(){
@Override
public boolean accept(File file) {
if(file.isDirectory())
return true;
int length=file.getName().length();
if(length5)
return false;
if(file.getName().substring(length-4).equals(".txt"))
return true;
return false;
}
@Override
public String getDescription() {
return "文本文件";
}
});
chooser.showSaveDialog(Application2.this);
File file=chooser.getSelectedFile();
if(file==null)
return;
try {
PrintWriter pw=new PrintWriter(file);
pw.print(output1.getText());
pw.flush();
pw.print(output2.getText());
pw.flush();
}catch (IOException e1) {
e1.printStackTrace();
}
}
});
JPanel buttonPane=new JPanel();
buttonPane.add(open);
buttonPane.add(save);
this.add(buttonPane,BorderLayout.SOUTH);
}
public String[] getSentences(){
ArrayListString set=new ArrayListString();
int length=input.getText().length();
for(int i=0,last=0;ilength;i++){
String s=String.valueOf(input.getText().charAt(i));
if(s.equals("\n"))
last=i+1;
if(s.equals(".")||s.equals(",")||s.equals("。")||s.equals("。")||s.equals("!")||s.equals("?")||s.equals("?")||s.equals("!")||s.equals(",")){
set.add(input.getText().substring(last,i)+s);
last=i+1;
}
}
return set.StringtoArray(new String[set.size()]);
}
public String getKeySentences(String[] key,String[] sentences){
String result="";
A: for(int i=0;isentences.length;i++){
for (int k = 0; k key.length; k++)
if (sentences[i].contains(key[k].subSequence(0, key[k].length()))) {
result += sentences[i] + "\n";
continue A;
}
}
return result;
}
private JTextArea input;
private JTextArea output1;
private JTextArea output2;
private JButton open;
private JButton save;
private String[] keyWords1;
private String[] keyWords2;
public static void main(String... args){
EventQueue.invokeLater(new Runnable(){
public void run(){
new Application2().setVisible(true);
}
});
}
}