「java读汉字」java输入汉字
今天给各位分享java读汉字的知识,其中也会对java输入汉字进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java 怎样用readline读取文本文件的汉字
- 2、求助,java中怎么用字节流读写汉字
- 3、java 字节流读取汉字 和 字母的问题
- 4、JAVA中,如何从一个文件读取汉字,并写入另一文件中,要求遇到句号就换行,并记录逗号的个数。
- 5、在java中,如何从文件流中读入一个个汉字,不许用readLine方法
- 6、java中如何从txt文件中一行一行读取汉字,再存到另一txt文件中
java 怎样用readline读取文本文件的汉字
import java.io.FileReader;
public class reader {
public static void main(String args[]) throws Exception{
FileReader a=new FileReader("1.txt");
int c;
while((c=a.read())!=-1){
System.out.print((char)c);
}
a.close();
}
}
去试试吧
求助,java中怎么用字节流读写汉字
读汉字文件一般用字符流或文件流,这样就很好规避你说的那种问题了,wo反正是不用字节流来读取有汉字的文件了,因为wo 也不清楚不同字符编码下,汉字是怎么样的一种情况了
java 字节流读取汉字 和 字母的问题
java乱码问题是比较多见。
整个环节中只要有一项有问题,就会产生乱码。
1、读取的文件采用的编码。
2、jvm所使用的编码。这个要看你所使用的OS和版本,jvm会使用OS中设定的默认编码。比如你使用英文版的winxp,jvm在处理流的时候就会默认采用cp什么什么的编码。
3、如果你使用了中间件,比如tomcat这样的web server,在request和response中,tomcat会自动使用ISO-8859-1编码。
4、如果有数据库数据交换,数据库中使用的编码。
你确认所有的环节用得都是同一种编码,就可以解决乱码问题了。
如果不是相同编码,那就做转码处理。
转码例句:
String s2 = new String(s1.getBytes("UTF-8"), "ISO8859_1");
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中,如何从文件流中读入一个个汉字,不许用readLine方法
指定字符集,GB2312
InputStreamReader(InputStream in,GB2312)
具体方法我就不给你实现了~
有问题再问我吧~
-----------------------------------------------------------
不好意思现在才给你,刚才洗了个澡~
代码给你贴出来了,你看符合你的要求不~
有问题随时问我,知道的都告诉你~
-----------------------------------------------------------
import java.io.*;
//先在你要编译的当前文件夹下建一个文本,里面给定你要写的内容,文件名为a.txt
public class ReadTest{
public static void main(String[] args){
DataInputStream din = null;
try{
//定义一个输入流,把当前文件夹下的a.txt里的内容以GB2312字符集读出
InputStreamReader is = new InputStreamReader(new FileInputStream("a.txt"),"GB2312");
String str1 = "";
String str2 = "";
//循环读每一个字符,并连接在一个字符串str1上
int i =-1;
while ((i = is.read()) != -1)
{
char m = (char)i;
str2=str2.valueOf(m);
str1 = str1+str2;
}
//对字符串我还是给你做了简单的操作,你可以根据自己的要求修改
str1 = str1.replaceAll(",","\n");//把所有逗号替换为 换行(标点为全角)
str1 = str1.replaceAll("。","\n");//把所有句号替换为 换行
System.out.println(str1); //打印输出
}catch(FileNotFoundException e){//异常捕获
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
finally{//最终行为---关闭流~
try{
if (din != null){
din.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
};
java中如何从txt文件中一行一行读取汉字,再存到另一txt文件中
首先用FileReader
fileReader=new
FileReader(路径)来创建一个节点流,
然后用BufferedReader
reader=new
BufferedReader(fileReader),以BufferederReader处理流来包装这个节点流,然后调用
BufferedReader类里面的readLine()方法就可以一行一行地读了。
至于存到另一个txt文件中,
你把上面输入流读到的东西放到一个字符串Strng
里面,然后用FileWriter类建立对象,用它的write(String
str)方法一次写到目标文件就可以了。
java读汉字的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java输入汉字、java读汉字的信息别忘了在本站进行查找喔。