Java读取CSV文件指定行的值
2010-01-27 22:35
525 查看
测试txt路径:C:/testlog.txt,内容如下:
23:25:37 VirtualDisk - couldn't load BBArchive.dll - 126
23:29:37 VirtualDisk - couldn't load BBArchive.dll - 126
22:29:12 VirtualDisk - couldn't load BBArchive.dll - 126
22:35:17 VirtualDisk - couldn't load BBArchive.dll - 126
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
lllllllllllllllllllllllllllllllllllllllll
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
测试代码:
/*
* $RCSfile: Test.java,v $
* $Revision: 1.1 $
* $Date: 2009-2-20 $
*
* Copyright (C) 2005 Bettem, Inc. All rights reserved.
*
* This software is the proprietary information of Bettem, Inc.
* Use is subject to license terms.
*/
package servlet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* Title: Test
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* @author 李晗
* @version 1.0
*/
public class Test {
/**
* @param args
*/
/**
* 读取txt内容到数组
*/
public static String[] getTxtContent(String path)
{
File f = null;
String[] a = null;
try {
a = new String[100000];
f=new File(path);
InputStreamReader read = new InputStreamReader(new FileInputStream(f), "GBK");
BufferedReader reader = new BufferedReader(read);
String line;
int i;
for (i = 0; i < 100000; i++)
{
if ((line = reader.readLine()) != null)
{
a[i] = line;
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return a;
}
/**
* 读取指定一行内容
* @param path
* @param row
* @return
*/
public String listTxtByRow1(String path,Integer row)
{
String[] s = getTxtContent(path);
return "第"+row+"行:"+s[row-1];
}
public List<String> listTxtByRow2(String path,Integer start,Integer end)
{
List<String> list =new ArrayList<String>();
String[] s = getTxtContent(path);
for(int i = start;i <= end;i++)
{
list.add(s[i-1]);
}
return list;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.listTxtByRow1("C:/testlog.txt",6));//取出第6行数据
System.out.println("==================取出指定行数=====================");
List<String> list = t.listTxtByRow2("C:/testlog.txt", 2, 5);//取出2-5行数据
for(int i = 0;i<list.size();i++)
{
System.out.println(list.get(i));
}
} }
运行情况:
第6行:wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
==================取出指定行数=====================
23:29:37 VirtualDisk - couldn't load BBArchive.dll - 126
22:29:12 VirtualDisk - couldn't load BBArchive.dll - 126
22:35:17 VirtualDisk - couldn't load BBArchive.dll - 126
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
23:25:37 VirtualDisk - couldn't load BBArchive.dll - 126
23:29:37 VirtualDisk - couldn't load BBArchive.dll - 126
22:29:12 VirtualDisk - couldn't load BBArchive.dll - 126
22:35:17 VirtualDisk - couldn't load BBArchive.dll - 126
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
lllllllllllllllllllllllllllllllllllllllll
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
测试代码:
/*
* $RCSfile: Test.java,v $
* $Revision: 1.1 $
* $Date: 2009-2-20 $
*
* Copyright (C) 2005 Bettem, Inc. All rights reserved.
*
* This software is the proprietary information of Bettem, Inc.
* Use is subject to license terms.
*/
package servlet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* Title: Test
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* @author 李晗
* @version 1.0
*/
public class Test {
/**
* @param args
*/
/**
* 读取txt内容到数组
*/
public static String[] getTxtContent(String path)
{
File f = null;
String[] a = null;
try {
a = new String[100000];
f=new File(path);
InputStreamReader read = new InputStreamReader(new FileInputStream(f), "GBK");
BufferedReader reader = new BufferedReader(read);
String line;
int i;
for (i = 0; i < 100000; i++)
{
if ((line = reader.readLine()) != null)
{
a[i] = line;
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return a;
}
/**
* 读取指定一行内容
* @param path
* @param row
* @return
*/
public String listTxtByRow1(String path,Integer row)
{
String[] s = getTxtContent(path);
return "第"+row+"行:"+s[row-1];
}
public List<String> listTxtByRow2(String path,Integer start,Integer end)
{
List<String> list =new ArrayList<String>();
String[] s = getTxtContent(path);
for(int i = start;i <= end;i++)
{
list.add(s[i-1]);
}
return list;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.listTxtByRow1("C:/testlog.txt",6));//取出第6行数据
System.out.println("==================取出指定行数=====================");
List<String> list = t.listTxtByRow2("C:/testlog.txt", 2, 5);//取出2-5行数据
for(int i = 0;i<list.size();i++)
{
System.out.println(list.get(i));
}
} }
运行情况:
第6行:wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
==================取出指定行数=====================
23:29:37 VirtualDisk - couldn't load BBArchive.dll - 126
22:29:12 VirtualDisk - couldn't load BBArchive.dll - 126
22:35:17 VirtualDisk - couldn't load BBArchive.dll - 126
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
相关文章推荐
- java读取csv文件内容示例代码
- java读取csv文件
- 使用pandas读取csv文件的指定列
- java读取csv文件
- Java中框架的概念,以及从配置文件中读取配置信息并加载指定类
- java读取csv文件
- Java如何实现按指定行读取文件
- java按指定编码写入和读取文件内容的类
- Java读取指定目录下的文件内容
- java 使用IO流读取指定文件中的内容
- Java中实现读取文件指定行
- pandas读取csv文件的指定列
- java读取写入csv文件Demo
- Java项目读取配置文件时,FileNotFoundException 系统找不到指定的文件,System.getProperty("user.dir")的理解
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
- Java中实现读取文件指定行
- Java按指定行数读取文件
- java按指定编码写入和读取文件内容的
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
- Java读取csv文件