SUN认证考试知识点:java读取文件内容的函数

2016-10-31 00:00:00嘉辉 SUN认证

  public static String readFile(String fileName) {

  String output = "";

  File file = new File(fileName);

  if (file.exists()) {

  if (file.isFile()) {

  try {

  BufferedReader input = new BufferedReader(new FileReader(

  file));

  StringBuffer buffer = new StringBuffer();

  String text;

  while ((text = input.readLine()) != null)

  buffer.append(text + "\n");

  output = buffer.toString();

  } catch (IOException ioException) {

  System.err.println("File Error!");

  }

  } else if (file.isDirectory()) {

  String[] dir = file.list();

  output += "Directory contents:\n";

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

  output += dir[i] + "\n";

  }

  }

  } else {

  System.err.println("Does not exist!");

  }

  return output;

  }

  public static void main(String args[]) {

  Map ab = new HashMap();

  String str = readFile("C:/AbCircuitInfo");

  String[] temp=str.split("\\n");

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

  ab.put(temp[i].substring(0,temp[i].indexOf(",")), temp[i].substring(temp[i].indexOf(",")+1,temp[i].length()));

  }

  }

[SUN认证]相关推荐

[SUN认证]相关栏目推荐
查看更多
上一篇:Java如何实现简单的whois查询 下一篇:SUN认证考试知识点:OSCache提升J2EE系统运行性能