JAVA中,如何从一个文件读取汉字,并写入另一文件中,要求遇到句号就换行,并记录逗号的个数。

JAVA中,如何从一个文件读取汉字,并写入另一文件中,要求遇到句号就换行,并记录逗号的个数。,第1张

import java.io.BufferedWriter

import java.io.File

import java.io.FileNotFoundException

import java.io.FileWriter

import java.io.IOException

import java.sql.ResultSet

import java.text.SimpleDateFormat

import java.util.Calendar

import java.util.Date

import com.talk2yp.DBConn.DBConnect

public class ReadFile {

public ReadFile() {

}

/**

* 读某返型个文件夹下的所有文件夹和文件

*

* @param delpath

*String

* @throws FileNotFoundException

* @throws IOException

* @return boolean

*/

public static boolean readfile(String filepath,String writePath )

throws FileNotFoundException, IOException {

try {

File file = new File(filepath)

if (!file.isDirectory()) {

System.out.println("文件")

System.out.println("path=" + file.getPath())

System.out.println("absolutepath=" + file.getAbsolutePath())

System.out.println("name=" + file.getName())

} else if (file.isDirectory()) {

System.out.println("文件夹")

String[] filelist = file.list()

File writeFile= new File(writePath)

if (writeFile.exists()) {

System.out.println("文件存在")

} else {

System.out.println("文件不存在,正在创建...")

if (writeFile.createNewFile()) {

System.out.println("文件创建成功!")

} else {

System.out.println("文件创建失败!"旁孝)

}

}

BufferedWriter output = new BufferedWriter(new FileWriter(writeFile))

String s1 = new String()

for (int i = 0i <filelist.lengthi++) {

File readfile = new File(filepath + "\\" + filelist[i])

if (!readfile.isDirectory()) {

System.out.println("path=" + readfile.getPath())

System.out.println("absolutepath="

+ readfile.getAbsolutePath())

System.out.println("name=" + readfile.getName())

//====重命名======

//设置修改后图片的名称漏启猜。。格式 日期+随机数

Calendar thismonth=Calendar.getInstance()

String year=String.valueOf(thismonth.get(Calendar.YEAR))

String month=String.valueOf(thismonth.get(Calendar.MONTH)+1)

String day = String.valueOf(thismonth.get(Calendar.DATE))

String mytimess=year+month+day

int random = (new java.util.Random()).nextInt(100000000)

String myFileName=mytimess+random+ readfile.getName().substring( readfile.getName().lastIndexOf("."))

System.out.println("rename="+myFileName)

File f=new File(filepath+"/"+myFileName)

//===============

//======插入数据库=========

try {

String[] name=readfile.getName().split("\\.")

DBConnect selectConn=new DBConnect()

DBConnect updateConn=new DBConnect()

ResultSet rs=null

String sql="select * from com_company_1 where name='"+name[0]+"'"

rs= selectConn.executeQuery(sql)

if(rs.next()){

String sql1="update com_business_1 set picurl='/upload/test/"+myFileName+"' where company_id ="+rs.getInt("id")

updateConn.executeUpdate(sql1)

System.out.println(rs.getString("name"))

System.out.println("fileNO----------->"+i)

//========记录已经修改的图片============

s1+=readfile.getName()+" "+myFileName+"\n"

output.write(s1)

readfile.renameTo(f)//重命名文件

//========记录已经修改的图片============

}

} catch (Exception e) {

e.printStackTrace()

}

//======插入数据库=========

System.out.println("fileSize----------->"+filelist.length)

} else if (readfile.isDirectory()) {

readfile(filepath + "\\" + filelist[i], writePath)

}

}

//System.out.println("============"+s1)

// //output.write(s1)

//System.out.println("======333333333======")

//System.out.println(filelist.length)

}

} catch (FileNotFoundException e) {

System.out.println("readfile() Exception:" + e.getMessage())

}

return true

}

public static void main(String[] args) {

for(int i=1i<=10i++){

System.out.println(i)

}

// for(int i=0i<100i++){

// System.out.println(new SimpleDateFormat("yyyyMMddHHmmssSSS").format( new Date() ) )

//System.out.println(System.currentTimeMillis())

// int random = (new java.util.Random()).nextInt(100000000)

// System.out.println(random)

//System.out.println("******************************")

// }

//

// //write("d:/123.txt", "hello")

}

/**

* 删除某个文件夹下的所有文件夹和文件

*

* @param delpath

*String

* @throws FileNotFoundException

* @throws IOException

* @return boolean

*/

// public static boolean deletefile(String delpath)

// throws FileNotFoundException, IOException {

// try {

//

// File file = new File(delpath)

// if (!file.isDirectory()) {

//System.out.println("1")

//file.delete()

// } else if (file.isDirectory()) {

//System.out.println("2")

//String[] filelist = file.list()

//for (int i = 0i <filelist.lengthi++) {

// File delfile = new File(delpath + "\\" + filelist[i])

// if (!delfile.isDirectory()) {

// System.out.println("path=" + delfile.getPath())

// System.out.println("absolutepath="

//+ delfile.getAbsolutePath())

// System.out.println("name=" + delfile.getName())

// delfile.delete()

// System.out.println("删除文件成功")

// } else if (delfile.isDirectory()) {

// deletefile(delpath + "\\" + filelist[i])

// }

//}

//file.delete()

//

// }

//

// } catch (FileNotFoundException e) {

// System.out.println("deletefile() Exception:" + e.getMessage())

// }

// return true

// }

// ===写文件======================

////public static void write(String path, String content) {

//// String s = new String()

//// String s1 = new String()

//// try {

//// File writeFile= new File(path)

//// if (writeFile.exists()) {

//// System.out.println("文件存在")

//// } else {

//// System.out.println("文件不存在,正在创建...")

//// if (writeFile.createNewFile()) {

////System.out.println("文件创建成功!")

//// } else {

////System.out.println("文件创建失败!")

//// }

////

//// }

//// BufferedReader input = new BufferedReader(new FileReader(writeFile))

////

//// while ((s = input.readLine()) != null) {

//// s1 += s + "\n"

//// }

//// System.out.println("文件内容:" + s1)

//// input.close()

//// s1 += content

////

//// BufferedWriter output = new BufferedWriter(new FileWriter(writeFile))

//// output.write(s1)

//// output.close()

//// } catch (Exception e) {

//// e.printStackTrace()

//// }

////}

//

//// ===读文件==================

//public static void read(String file) {

// String s = null

// StringBuffer sb = new StringBuffer()

// File f = new File(file)

// if (f.exists()) {

// System.out.println("文件存在")

//

// try {

// BufferedReader br = new BufferedReader(new InputStreamReader(

// new FileInputStream(f)))

//

// while ((s = br.readLine()) != null) {

//sb.append(s)

// }

// System.out.println(sb)

// } catch (Exception e) {

// e.printStackTrace()

// }

// } else {

// System.out.println("文件不存在!")

// }

//}

}

import java.io.*

public class Copy {

public static void main(String args[]){

if(args.length!=2){

System.out.println("参数不正确,无法袭森弯完成复制!正确用法春禅:")

System.out.println("java Copy 源文件名 目的文件名")

System.exit(0)

}

copyFile(args[0],args[1])

}

public static void copyFile(String src,String obj){

FileInputStream fis=null

FileOutputStream fos=null

try{

fis=new FileInputStream(src)

fos=new FileOutputStream(obj)

}catch(FileNotFoundException e){

System.out.println("文件不存在,请检查拍闷您的输入:")

}catch(IOException e){

e.printStackTrace()

}

try{

int b

while((b=fis.read())!=-1){

fos.write(b)

}

fos.flush()

System.out.println("文件复制成功!")

}catch(IOException e){

System.out.println("文件写入错误!")

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8276138.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存