码家

Web Platform, Cloud and Mobile Application Development

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

package

extractors;

import

java.io.ByteArrayOutputStream;

import

java.io.File;

import

java.io.FileInputStream;

 

public

classExtractorTXT {

public ExtractorTXT() {

}

public String toString(File f) {

String result =

" ";

try {

FileInputStream in =

new FileInputStream(f);

byte[] b = null;

ByteArrayOutputStream out =

new ByteArrayOutputStream();

while (in.available() != 0) {

out.write(in.read());

}

b = out.toByteArray();

if (b != null) {

result =

new String(b);

}

in.close();

out.close();

}

catch (Exception ex) {

System.

out.println("Read fail! ");

}

return result;

}

public void fromatFile(String[] list) {

try {

for (int i = 0; i < list.length; i++) {

String one = (String) list[i];

String[] contents = one.split(

",");

System.

out.println("Employee->" + (i + 1));

System.

out.println("Employee Name:" + contents[0]);

System.

out.println("Employee ID:" + contents[1]);

System.

out.println("Employee Department:" + contents[2]);

}

}

catch (Exception ex) {

ex.printStackTrace();

}

}

public static void main(String args[]) {

File file =

new File("D:\\TxtExtractor.txt ");

ExtractorTXT t =

new ExtractorTXT();

String str = t.toString(file);

String[] s = str.split(

"\\.");

System.

out.println(s.length);

System.

out.println(str);

t.fromatFile(s);

}

}

posted on 2011-10-17 10:50  海山  阅读(223)  评论(0编辑  收藏  举报