您的位置:首页 > 编程语言 > Java开发

Java 读取csv文件

2017-01-20 16:24 483 查看
public static void read(String path) {
File file = new File(path);
if (!file.exists() || file.isDirectory()) {
System.out.println(path);
System.out.println("文件不存在或是path为文件夹");
return;
}

BufferedReader br = null;
try {
/**
* csv文件每个单元格使用,分割
* 默认ANSI编码
*/
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
String msg = "";
msg = br.readLine();
while (msg!=null) {
System.out.println(msg);
msg = br.readLine();
}
} catch (FileNotFoundException e) {
System.out.println("创建流文件异常:文件没找到");
e.printStackTrace();
} catch (IOException e) {
System.out.println("流文件读取异常:" + e.toString());
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
System.out.println("流文件关闭出现异常:" + e.toString());
e.printStackTrace();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: