java 文件读写实例(读写csv文件)
不能直接用 File file = new File("http://127.0.0.1:8080/aa.txt") 来读取,因为网络上的传输协议为HTTP,与本地不同,要用URL来读取
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5577')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5577> String output="";
File file = new File("E://bb.txt");
URL MyURL = new URL("http://127.0.0.1:8080/aa.txt");
URLConnection uc=MyURL.openConnection();
uc.connect();
InputStreamReader _Input=new InputStreamReader(uc.getInputStream(),"UTF-8");
BufferedReader br=new BufferedReader(_Input);
String s="";
while((s=br.readLine())!=null){
output =s;
}
FileWriter fw=new FileWriter(file);
fw.write(output);
fw.flush();
fw.close();
如果你要读写大文件我们上面办法可能实例不了
我开始用的读入方式如下:
BufferedReader br = new BufferedReader(new FileReader(this.datafile));
String line;
int numofline = 0;
while(br.ready()){…}
用bufferedreader方式来读写大文件是没有任何问题的,最后经过仔细研究问题发生在while循环的判断条件上。ready()是判断输入流中止,并不一定是文件的结尾。判断文件的结尾应该是用下面的语句:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4220')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4220>(line = br.readLine()) != null
这样才能将原来的文件读完。原来的语句只读了源文件很少一部分内容。
opencsv读写csv文件
OpenCSV是一个简单的用于解析CSV文件的java类库,它封装了CSV格式文件的输出和读入,可以自动处理CSV格式中的特殊字符,最重要的是OpenCSV可以用于商业化(commercial-friendly)。
具体的使用方法:
读CSV文件
1、使用Iterator方式读
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] nextLine[1] "etc...");
}
2、使用List
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1531')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1531>CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();
写CSV文件
1、类似于FileReader
CSV
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4052')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4052>Writer writer = new CSVWriter(new FileWriter("yourfile.csv"), 't');// feed in your array (or convert your data to an array)
String[] entries = "first#second#third".split("#");
writer.writeNext(entries);
writer.close();
您可能感兴趣的文章:
php读取与下载csv文件的示例代码
php读取csv、写入csv与导出csv文件
php导出与读取csv文件的实现代码
php读取csv文件内容的实例代码
java 文件读写实例(读写csv文件)
php读取(打开)csv文件的小例子
php读取csv文件怎么去掉双引号
php生成(导出)csv文件的函数示例
PHP读取csv文件内容的几个例子
CSV格式读取表格插件 csv2table