java文本分类训练代码,java文本分类训练代码有哪些

如何用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 (length  Integer.MAX_VALUE) // 当文件的长度超过了int的最大值

{

System.out.println("this file is max ");

return null;

}

bytes = new byte[length];

int offset = 0;

int numRead = 0;

while (offset  bytes.length

 (numRead = is.read(bytes, offset, bytes.length - offset)) = 0) {

offset += numRead;

}

// 如果得到的字节长度和file实际的长度不一致就可能出错了

if (offset  bytes.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] == -1  head[1] == -2)

code = "UTF-16";

if (head[0] == -2  head[1] == -1)

code = "Unicode";

if ((head[0] == -17  head[1] == -69  head[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 != null  file.isFile()) {

String filename = file.getName();

int i = filename.lastIndexOf(".");

if (i  0  i  filename.length() - 1) {

return filename.substring(i + 1).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; i  files.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代码示例

importjava.awt.*;importjava.awt.event.*;classShopFrameextendsFrameimplementsActionListener{Labellabel1,label2,label3,label4;Buttonbutton1,button2,button3,button4,button5;TextAreatext;Panelpanel1,panel2;staticfloatsum=0.0f;ShopFrame(Strings){super(s);setLayout(newBorderLayout());label1=newLabel("面纸:3元",Label.LEFT);label2=newLabel("钢笔:5元",Label.LEFT);label3=newLabel("书:10元",Label.LEFT);label4=newLabel("袜子:8元",Label.LEFT);button1=newButton("加入购物车");button2=newButton("加入购物车");button3=newButton("加入购物车");button4=newButton("加入购物车");button5=newButton("查看购物车");text=newTextArea("商品有:"+"\n",5,10);text.setEditable(false);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this);button5.addActionListener(this);panel1=newPanel();panel2=newPanel();panel1.add(label1);panel1.add(button1);panel1.add(label2);panel1.add(button2);panel1.add(label3);panel1.add(button3);panel1.add(label4);panel1.add(button4);panel2.setLayout(newBorderLayout());panel2.add(button5,BorderLayout.NORTH);panel2.add(text,BorderLayout.SOUTH);this.add(panel1,BorderLayout.CENTER);this.add(panel2,BorderLayout.SOUTH);setBounds(100,100,350,250);setVisible(true);validate();}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button1){text.append("一个面纸、");sum=sum+3;}elseif(e.getSource()==button2){text.append("一只钢笔、");sum=sum+5;}elseif(e.getSource()==button3){text.append("一本书、");sum=sum+10;}elseif(e.getSource()==button4){text.append("一双袜子、");sum=sum+8;}elseif(e.getSource()==button5){text.append("\n"+"总价为:"+"\n"+sum);}}}publicclassShopping{publicstaticvoidmain(String[]args){newShopFrame("购物车");}}我没用Swing可能显示不出来你的效果。不满意得话我在给你编一个。

有人用java写过文本分类系统吗

文本分类的重点不在分类,而在怎么样描述文本模型和提取文档中的terms并把它数字化,转化为分类器可以使用的输入类型。这前面的处理直接关系到后面分分类效果。就文本模型而言,现在普遍使用的还是Salton 和 McGill的Vector Space Model, 通过TF-IDF统计,如果是英文的话,要进行语义层次的抽象, 这方面可以使用WordNet,网上可以找到JAVA wordnet 的API,,个人认为比较好的是JWNL 如果是中文的话,涉及到分词, 中科院计算所分词系统ICTCLAS, 可到其网站上下载免费版。

至于JAVA写的分类器很多,常用的比如说Weka, RapidMiner(这个相当不错,有专门的Web data Mining的扩展包,是我的最爱),这些都可以自己调用其接口实现新的算法。个人强烈推荐使用RapidMiner,功能相当强大,几乎实现了当前的所有机器学习的算法,并且操纵简便。

学习提示:不要刚开始就希望效果很好,急躁是做学问的大敌,肯定会出现一些意想不到的问题,最主要是勤于思考,善于查找问题,慢慢改进,一个问题,如果比较难,回到它最原始最简单的问题上去。祝你学业进步。

java文本框格式的代码

import java.awt.Color;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.Iterator;

import javax.imageio.IIOImage;

import javax.imageio.ImageIO;

import javax.imageio.ImageWriter;

import javax.imageio.stream.ImageOutputStream;

public class Ttex {

BufferedImage bi = new BufferedImage(300, 200, BufferedImage.TYPE_INT_RGB);

public Ttex(String text) {

Graphics gr = bi.createGraphics();

gr.setColor(Color.white);

gr.fillRect(0, 0, bi.getWidth(), bi.getHeight());

gr.setColor(Color.GRAY);

gr.drawString(text, 10, 10);

}

/**

* 根据地址来保存文件

*

* @param dir

* 文件地址

*/

private void Save(String dir) {

BufferedImage image = bi;

// 得到系统支持的图片写入器

Iterator it = ImageIO.getImageWritersBySuffix("jpg");

ImageWriter iw = null;

if (it.hasNext()) {

try {

ImageOutputStream imgOut = ImageIO

.createImageOutputStream(new File(dir));

iw = (ImageWriter) it.next();

iw.setOutput(imgOut);

IIOImage iioi = new IIOImage(image, null, null);

iw.write(iioi);

imgOut.close();

iw.dispose();

} catch (IOException e1) {

}

}

}

public static void main(String[] args) {

Ttex m = new Ttex("0123456789");

m.Save("D:/1.jpg");

}

}

Java实验,代码怎么写?

Shape.java接口代码

public interface Shape {

public static final double PI = 3.14d;

public double area();

}

Circle.java圆类代码

public class Circle implements Shape {

private double radius;

public Circle(double radius) {

  this.radius = radius;

}

@Override

public double area() {

  return PI * this.radius * this.radius;

}

public double perimeter() {

  return 2 * PI * this.radius;

}

}

Cylinder.java圆柱体类代码

public class Cylinder extends Circle {

private double height;

public Cylinder(double radius, double height) {

  super(radius);

  this.height = height;

}

public double area() {

  return 2 * super.area() + super.perimeter() * this.height;

}

public double volume() {

  return super.area() * this.height;

}

}

X5_3_6.java主类代码

public class X5_3_6 {

public static void main(String[] args) {

  Circle cir1 = new Circle(5);

  System.out.println("圆的面积为:" + cir1.area());

  System.out.println("圆的周长为:" + cir1.perimeter());

  Cylinder cy1 = new Cylinder(10, 15);

  System.out.println("圆柱体的表面积为:" + cy1.area());

  System.out.println("圆柱体的体积为:" + cy1.volume());

}

}

上面是我写的代码,下图是执行结果,麻烦看一下,是否可以。


网站标题:java文本分类训练代码,java文本分类训练代码有哪些
分享链接:http://ybzwz.com/article/dsssjcj.html