比如 String msg = "中文abc"
msg.getBytes("UNICODE").length; 結果是12
msg.getBytes("ISO8859-1").length; 結果是5
msg.getBytes("GBK").length; 結果是7
public static void writeFile(String content,String comspec)
{
try
{
FileOutputStream fos = new FileOutputStream(comspec);

 

Writer out = new OutputStreamWriter(fos,ENCODE);
out.write(content);
float size = (float)content.getBytes(ENCODE).length/1024;//獲得輸出字串位元組數ENCODE為檔編碼
DecimalFormat df = new DecimalFormat("0.00");
String filesize = df.format(size);
System.out.println("輸出檔案:" + comspec + " 大小:" + filesize + "K " + content.getBytes(ENCODE).length + "B" +"\r\n");
out.close();
fos.close();
} catch (IOException e)
{
System.out.println("寫檔操作出錯!");
e.printStackTrace();
}
}

 

public static String readFile(String filePath)
{
StringBuilder sb = new StringBuilder();
try
{
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
float size = (float)fis.available()/1024;//獲得檔案大小
DecimalFormat df = new DecimalFormat("0.00");
String filesize = df.format(size);
System.out.println("原始檔案:" + filePath + " 大小:" + filesize + "K " + fis.available() + "B");
InputStreamReader read = new InputStreamReader (fis,ENCODE);
BufferedReader reader=new BufferedReader(read);
String s = reader.readLine();
while (s != null)
{
sb.append(s);
sb.append("\r\n");
s = reader.readLine();
}
reader.close();
} catch (IOException e)
{
e.printStackTrace();
}
return sb.toString();
}
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()