首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

javaIO资料读写操作处理文件乱码

2012-06-27 
javaIO文件读写操作处理文件乱码? 获得文件大小,可以进行文件大小的判断。public void getFileLength(){Str

javaIO文件读写操作处理文件乱码

? 获得文件大小,可以进行文件大小的判断。

public void getFileLength(){String str = "D:\\testPath\\testFile.txt";  File file = new File(str);  FileInputStream fis;  try {     fis = new FileInputStream(file);      System.out.println("文件size:"+String.valueOf(fis.available()/1024/1024)+"M");  } catch (FileNotFoundException e) {      e.printStackTrace();  } catch (IOException e) {      e.printStackTrace();  }}

??

?

? 文件乱码处理:读写操作时添加编码格式。

??? 读文件,把一个大文件拆成小文件

  public LogLockFile split(File file) throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));while ((txt = reader.readLine()) != null) {            if ("" == txt.trim() || "".equals(txt.trim()) || txt.trim() == null) {                 continue;              }             // 去#开头的行和空行            if (!"".equals(txt) && txt != null) {                  if (txt.indexOf("#") == 0) {                         continue;                 }               list.add(txt);                n++;             }           if (n == DEFAULT_FILE_LENGTH) {//满足长度写入文件                   writerFile(list, logLockFile.getPath(), tempName + getStringFill(page) + "." + tempType);                   n = 0;                 page++;                  list = new ArrayList<String>();              }       }   }

?

?

??? 写文件方法

private void writerFile(List<String> list, String path, String name) {        try {File f = new File(path + "\" + name);   BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));   for (int i = 0; i < list.size(); i++) {   String str = list.get(i);   if (str == null) {    break;    }  output.write(str);  output.write("\r\n"); }  output.close();       } catch (Exception e) {   e.printStackTrace();       }}

?

热点排行
Bad Request.