RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
文本分类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读取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);

}

自己测试一下:这是我的输出结果

Console:

One=190 ;Two=0 ;Three=410

javascript和java有什么区别、java的分类!

二者的区别体现在:

首先,它们是两个公司开发的不同的两个产品,Java是SUN公司推出的新一代面向对象的程序设计语言,特别适合于Internet应用程序开发;而JavaScript是Netscape公司的产品,其目的是为了扩展Netscape Navigator功能,而开发的一种可以嵌入Web页面中的基于对象和事件驱动的解释性语言。

其次,JavaScript是基于对象的,而Java是面向对象的,即Java是一种真正的面向对象的语言,即使是开发简单的程序,必须设计对象。JavaScript是种脚本语言,它可以用来制作与网络无关的,与用户交互作用的复杂软件。它是一种基于对象和事件驱动的编程语言。因而它本身提供了非常丰富的内部对象供设计人员使用。

第三,两种语言在其浏览器中所执行的方式不一样。Java的源代码在传递到客户端执行之前,必须经过编译,因而客户端上必须具有相应平台上的仿真器或解释器,它可以通过编译器或解释器实现独立于某个特定的平台编译代码的束缚。JavaScript是一种解释性编程语言,其源代码在发往客户端执行之前不需经过编译,而是将文本格式的字符代码发送给客户,由浏览器解释执行。

第四,两种语言所采取的变量是不一样的。Java采用强类型变量检查,即所有变量在编译之前必须作声明。JavaScript中变量声明,采用其弱类型。即变量在使用前不需作声明,而是解释器在运行时检查其数据类型。

第五,代码格式不一样。Java是一种与HTML无关的格式,必须通过像HTML中引用外媒体那么进行装载,其代码以字节代码的形式保存在独立的文档中。JavaScript的代码是一种文本字符格式,可以直接嵌入HTML文档中,并且可动态装载。编写HTML文档就像编辑文本文件一样方便。

第六,嵌入方式不一样。在HTML文档中,两种编程语言的标识不同,JavaScript使用 script.../script 来标识,而Java使用applet ... /applet来标识。

第七,静态绑定和动态绑定。Java采用静态联编,即Java的对象引用必须在编译时的进行,以使编译器能够实现强类型检查。

java如何将文本框内容的string类型转换为file类型

html

body

form action="/example/html/form_action.asp" method="get"

pFirst name: input type="text" name="fname" //p

pLast name: input type="file" name="lname" //p

input type="submit" value="Submit" /

/form

p请单击确认按钮,输入会发送到服务器上名为 "form_action.asp" 的页面。/p

/body

/html

请参考上面的代码

上面是文本 - string

下面是文件 - file


网页名称:文本分类java代码 java处理文本文件
网页路径:http://cqwzjz.cn/article/ddccjsp.html