「java读取汉字」java读取中文
今天给各位分享java读取汉字的知识,其中也会对java读取中文进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、JAVA中,如何从一个文件读取汉字,并写入另一文件中,要求遇到句号就换行,并记录逗号的个数。
- 2、java语言中如何获取字符串中汉字的个数
- 3、java中如何从txt文件中一行一行读取汉字,再存到另一txt文件中
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;i100;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("文件不存在!");
// }
//}
}
java语言中如何获取字符串中汉字的个数
【实例描述】
字符串中可以包含数字,字母、汉字或者其他字符。使用Character类的isDigit()方法可以判断字符中的某个字符是否为数字,使用Character类的isLetter()方法可以判断字符中的某个字符是否为字母。实例中将介绍一种方法用来判断字符串中的某个字符是否为汉字,通过此方法可以计算字符串中汉字的数量。实例的运行效果如图4.24所示。
【实现过程】
在Eclipse中新建项目ChineseCharacter,并在其中创建一个ChineseCharacter.java文件。在该类的主方法中创建标准输入流的扫描器对象,接收用户输入的字符串。我们在程序中使用matches()方法来统计该字符串中汉字的个数。核心代码如下所示:
protectedvoiddo_button_actionPerformed(ActionEvente){Stringtext=chineseArea.getText();//获取用户输入intamount=0;//创建汉字数量计数器for(inti=0;itext.length();i++){//遍历字符串每一个字符//使用正则表达式判断字符是否属于汉字编码booleanmatches=Pattern.matches("^[\u4E00-\u9FA5]{0,}$",""+text.charAt(i));if(matches){//如果是汉字amount++;//累加计数器}}umField.setText(amount+"");//在文本框显示汉字数量}
【代码解析】
本实例的关键点在于正则表达式的使用。Java提供了Pattern用于正则表达式的编译表示形式,该类提供的静态方法matches()可以执行正则表达式的匹配。该方法编译给定正则表达式并尝试给定输入与其匹配。如果要匹配的字符序列与正则表达式匹配则返回true,否则返回false。其声明语法如下:
publicstaticbooleanmatches(Stringregex,CharSequenceinput);【知识扩展】
使用正则表达式可以方便地进行字符串操作,正则表达式经常被用来验证用户输入的信息,如可以判断用户输入的格式是否正确。本实例中使用正则表达式来判断用户输入的字符串是否为汉字,如果为汉字则计数器加1,最后得到字符串中所有汉字的数量。
java中如何从txt文件中一行一行读取汉字,再存到另一txt文件中
首先用FileReader
fileReader=new
FileReader(路径)来创建一个节点流,
然后用BufferedReader
reader=new
BufferedReader(fileReader),以BufferederReader处理流来包装这个节点流,然后调用
BufferedReader类里面的readLine()方法就可以一行一行地读了。
至于存到另一个txt文件中,
你把上面输入流读到的东西放到一个字符串Strng
里面,然后用FileWriter类建立对象,用它的write(String
str)方法一次写到目标文件就可以了。
java读取汉字的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java读取中文、java读取汉字的信息别忘了在本站进行查找喔。
发布于:2022-11-30,除非注明,否则均为
原创文章,转载请注明出处。