如何从文件中读取中文(非unicode)到String中

seraph 2000-02-17 11:17:00
加精
如何从文件中读取中文(非unicode)到String中
使String中的内容为unicode的中文.
Thanks alot
...全文
669 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongfei 2000-06-26
  • 打赏
  • 举报
回复
thank you! seraph
hongfei 2000-03-28
  • 打赏
  • 举报
回复
please the elder bother seraph send some score to me !
I use a week in the problem.^v^
hongfei 2000-03-28
  • 打赏
  • 举报
回复
My code is here (use chinese params of sql to
read the chinese data from database):

(environment :visualage for java 2.0)

import java.sql.*;
/**
* This type was created in VisualAge.
*/
class MyTest {
/**
* This method was created in VisualAge.
* @param args java.lang.String[]
*/
public static void main(String args[]) {
Connection con = null;
Statement myState = null;
ResultSet myRSet = null;
String url, sName;
try {
url = "jdbc:odbc:paper";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, "", "");
myState = con.createStatement();
String sql = "select paperName from paper where author = \'张三\'";
byte[] ch_Byte = sql.getBytes();
sql = new String(ch_Byte,"iso-8859-1");
myRSet = myState.executeQuery(sql);
while (myRSet.next()) {
sName = myRSet.getString("paperName");
ch_Byte = sName.getBytes("iso-8859-1");
sName = new String(ch_Byte);
System.out.println("Name: " + sName);
}
} catch (Exception e) {
System.out.println("error: " + e.toString());
}
}
}
hongfei 2000-03-28
  • 打赏
  • 举报
回复
Now I have a way of solute chinese problem in java
(include jdbc-odbc bridge but as if only fit jdk 1.1.* and 1.0.*)
the way is using encoding way.

the "iso-8859-1" encoding is the most wide english encoding solution


supposing the chinese data is readed from file or database in String "sChData",
you must deal with the following code
" byte[] ch_byte = sChData.getBytes("iso-8859-1");
//here we force java vm to read chinese data in a english way
sChData = new String(ch_byte);
//and here we force to turn into unicode data",

in addition, when data writed to file or db , and use the following code
" byte[] ch_byte = sChData.getBytes();
//here we divide the string into byte array
sChData = new String(ch_byte,"iso-8859-1");
//and here we force to turn to the english way from
// the unicode byte array"

if you want to send data among threads, please use Reader/writer
in jdk 1.1.* or jdk 1.2.* to send the unicode data which are dealt
with the first way;

the way fit in all java editions except for jdbc in jdk1.2
(it is only now I have not solutions.)
Rich 2000-03-08
  • 打赏
  • 举报
回复
在JAVA1.1中,没有对应UNICODE的输入输出流类,只能用 String(byte[], "8859-1");解决,在1.2 中可以用Reader类解决.
bigchen 2000-03-04
  • 打赏
  • 举报
回复
用filereader类应该不会有问题,或者用new String(byte[],"GB2312")
ahfei 2000-02-26
  • 打赏
  • 举报
回复
jdbc支持中文,但jdbc-odbc bridge不支持中文,想我用interbase5,oracle8的pure jave driver都是支持中文的.
土豆 2000-02-18
  • 打赏
  • 举报
回复
西文肯定没问题,中文支持吗?据我所知,JDBC就不支持中文。
WHQ 2000-02-17
  • 打赏
  • 举报
回复
当成ASCII字符读进来不行吗?
blaise 2000-02-17
  • 打赏
  • 举报
回复
eager for score;-)
ahfei 2000-02-17
  • 打赏
  • 举报
回复
//下面程序需在jdk1.1以上使用,如果是jdk1.0,需按所述改动
import java.io.*;

public class Test3{

public static void main(String args[]){

try{

FileInputStream fis = new FileInputStream("Test.dat");

int len = fis.available();

byte[] b = new byte[len];

fis.read(b);

String s = new String(b);// 做 了 从GB 到Unicode 的 转换
//如果是jdk1.0,用 String s = new String(b, 0);
System.out.println(s); // 做 了 从Unicode 到GB 的 转 换

fis.close();

}catch(IOException e){

}

}

}


WHQ:真会灌水 :)

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧