2009年4月3日

Java Read/Write File

Read File
private String readFile(String fileName, String fileCharacter)
{
String line = "";
try
{
FileInputStream fis = new FileInputStream(new File(fileName));
// java.io.FileNotFoundException
BufferedReader br =new BufferedReader(new InputStreamReader(fis,fileCharacter));
// java.io.UnsupportedEncodingException
   
// java.io.IOException
while(br.ready())
line += br.readLine();

    br.close();
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}

return line;
}
Write File
private void writeFile(String fileName, byte[] writeText)
{
try
{
// Create file
File file = new File(fileName);
FileOutputStream fout = new FileOutputStream(file);
fout.write(writeText);
fout.close();

}
catch (Exception e)
{
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}

2009年4月2日

編碼轉換

byte[] String.getBytes(characterset);

getBytes() 回傳 string 在該平台的預設編碼的byte array
或者使用者自行指定編碼
ex.
String a = "殺很大";
bytes[] data = a.getBytes("UTF-8");


new String(byte[], characterset);

從byte array產生Unicode字串
若擔心猜錯byte array的編碼
則字行指定 characterset

2009年4月1日

Java

Install
Download from http://java.sun.com/javase/downloads/index.jsp
Click "Get the JDK download"

Select your Platform

Setting Envrionment Variable
In Windows
  • JAVA_HOME
C:\Program Files\Java\jdkX.X.X_XX

  • CLASSPATH
.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar;
  • PATH
.;%JAVA_HOME%\bin;.............