JAVA中,如何从一个文件读取汉字,并写入另一文件中,要求遇到句号就换行,并记录逗号的个数。
展开全部
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 = 0; i < filelist.length; i++) {
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=1; i<=10;i++){
System.out.println(i);
}
// for(int i=0;i<100;i++){
// 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 = 0; i < filelist.length; i++) {
// 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.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 = 0; i < filelist.length; i++) {
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=1; i<=10;i++){
System.out.println(i);
}
// for(int i=0;i<100;i++){
// 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 = 0; i < filelist.length; i++) {
// 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("文件不存在!");
// }
//}
}
展开全部
代码如下,注:关健是要用一个只有一个元素的char[]数组读出每个字符,再判断是逗号还是句号。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
public class ReadFile {
public static void main(String args[]){
char str[]=new char[1];//长度为1的char数组
File srcFile=new File("F:\\VS\\aa.txt");//原文件
File desFile=new File("F:\\VS\\bb.txt");//目标文件
String mid="";
int total=0;
try {
FileReader fr=new FileReader(srcFile);
FileWriter fw=new FileWriter(desFile);
while(fr.read(str)!=-1){//先读入数据一个一
// 个的读(一次读多少个由数组长度决定)
mid=String.valueOf(str);//变换为字符串
System.out.println(mid);
fw.append(mid);//写入新文件中
if(mid.equals("。"))
fw.append("\r\n");//遇到句号回车
if(mid.equals(","))
total++;//遇逗号记录
}
fw.append("\r\n"+"总共有"+total+"个逗号"+"\r\n");
System.out.println("总共有"+total+"个逗号");
fw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
public class ReadFile {
public static void main(String args[]){
char str[]=new char[1];//长度为1的char数组
File srcFile=new File("F:\\VS\\aa.txt");//原文件
File desFile=new File("F:\\VS\\bb.txt");//目标文件
String mid="";
int total=0;
try {
FileReader fr=new FileReader(srcFile);
FileWriter fw=new FileWriter(desFile);
while(fr.read(str)!=-1){//先读入数据一个一
// 个的读(一次读多少个由数组长度决定)
mid=String.valueOf(str);//变换为字符串
System.out.println(mid);
fw.append(mid);//写入新文件中
if(mid.equals("。"))
fw.append("\r\n");//遇到句号回车
if(mid.equals(","))
total++;//遇逗号记录
}
fw.append("\r\n"+"总共有"+total+"个逗号"+"\r\n");
System.out.println("总共有"+total+"个逗号");
fw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
很简单,直接读取文件。
然后String应该有个方法是replace();
把句号replace成“\n”即可
然后String应该有个方法是replace();
把句号replace成“\n”即可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先把所有的数据读出来,然后在判断有句号就换行,记录逗号的方法可以这样做,把数据保存在一个字符树组里,用循环判断树组里的元素是否为逗号,是就记录。思路就是这样,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我学的是c++,不过,给你个思想:以char读取进temp中,检测是否逗号或句号,是逗号,统计;再把temp压进数组是句号,压入数组,跳出循环(break;)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询