?? gbtobig5.java
字號:
// 例 5.4.5 GbToBig5.java
import java.io.*;
import java.util.*;
public class GbToBig5
{
static int iCharNum=0; // 用來統計讀取的字符個數
static String readInput(String strInFile)
{
StringBuffer buffer = new StringBuffer();
try
{
FileInputStream fis = new FileInputStream(strInFile);
// 以指定的編碼來創建字節流到字符流的轉換流
InputStreamReader isr = new InputStreamReader(fis, "GB2312");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1)
{
iCharNum += 1; // 統計讀取了多少字符
buffer.append((char)ch);
}
in.close();
return buffer.toString(); // 將讀取的字符串對象返回
}catch (IOException e) {
e.printStackTrace();
return null;
}
}
static void writeOutput(String str, String strOutFile)
{
try
{
FileOutputStream fos = new FileOutputStream(strOutFile);
// 以指定的編碼來創建字符流到字節流的轉換流
Writer out = new OutputStreamWriter(fos, "Big5");
out.write(str); // 將字符串的內容以指定的編碼寫入目標文件
out.close();
}catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
}
}
public static void main(String[] args)
{
System.out.println("Input GB2312 file, Output Big5 file.");
if (args.length!=2)
{
System.err.println("Usage: java GbToBig5 gbfile big5file");
System.exit(1);
}
String inputString = readInput(args[0]);
writeOutput(inputString,args[1]);
System.out.println("Number of Characters in file: "+iCharNum+".");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -