android怎样读文本文件的内容
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android怎样读文本文件的内容相关的知识,希望对你有一定的参考价值。
参考技术A 1. 读取操作String path = "/sdcard/foo.txt";
String content = ""; //文件内容字符串
//打开文件
File file = new File(path);
//如果path是传递过来的参数,可以做一个非目录的判断
if (file.isDirectory())
Toast.makeText(EasyNote.this, "没有指定文本文件!", 1000).show();
else
try
InputStream instream = new FileInputStream(file);
if (instream != null)
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
//分行读取
while (( line = buffreader.readLine()) != null)
content += line + "\n";
instream.close();
catch (java.io.FileNotFoundException e)
Toast.makeText(EasyNote.this, "文件不存在", Toast.LENGTH_SHORT).show();
catch (IOException e)
e.printStackTrace();
2. 写入操作
String filePath = "/sdcard/foo2.txt";
String content = "这是将要写入到文本文件的内容";
//如果filePath是传递过来的参数,可以做一个后缀名称判断; 没有指定的文件名没有后缀,则自动保存为.txt格式
if(!filePath.endsWith(".txt") && !filePath.endsWith(".log"))
filePath += ".txt";
//保存文件
File file = new File(filePath);
try
OutputStream outstream = new FileOutputStream(file);
OutputStreamWriter out = new OutputStreamWriter(outstream);
out.write(content);
out.close();
catch (java.io.IOException e)
e.printStackTrace();
java读取CSV文件
csv文件中带有双引号的字段。如"20091","sff","wqeq"字段,怎样读到数据库里面成为2009 sff wqeq三子段的值,现在我没有办法去掉那个引号。请教高手指点一下。
这是我写的个类 你参考下 其实那个引号是不用管的public class CsvUtil1
private String filename = null;
private BufferedReader bufferedreader = null;
private List list = new ArrayList();
public CsvUtil1()
public CsvUtil1(String filename) throws IOException
this.filename = filename;
bufferedreader = new BufferedReader(new FileReader(filename));
String stemp;
while ((stemp = bufferedreader.readLine()) != null)
list.add(stemp);
public List getList() throws IOException
return list;
public int getRowNum()
return list.size();
public int getColNum()
if (!list.toString().equals("[]"))
if (list.get(0).toString().contains(","))
return list.get(0).toString().split(",").length;
else if (list.get(0).toString().trim().length() != 0)
return 1;
else
return 0;
else
return 0;
public String getRow(int index)
if (this.list.size() != 0)
return (String) list.get(index);
else
return null;
public String getCol(int index)
if (this.getColNum() == 0)
return null;
StringBuffer scol = new StringBuffer();
String temp = null;
int colnum = this.getColNum();
if (colnum > 1)
for (Iterator it = list.iterator(); it.hasNext();)
temp = it.next().toString();
scol = scol.append(temp.split(",")[index] + ",");
else
for (Iterator it = list.iterator(); it.hasNext();)
temp = it.next().toString();
scol = scol.append(temp + ",");
String str = new String(scol.toString());
str = str.substring(0, str.length() - 1);
return str;
public String getString(int row, int col)
String temp = null;
int colnum = this.getColNum();
if (colnum > 1)
temp = list.get(row).toString().split(",")[col];
else if (colnum == 1)
temp = list.get(row).toString();
else
temp = null;
return temp;
public void CsvClose() throws IOException
this.bufferedreader.close();
public void test() throws IOException
CsvUtil1 cu = new CsvUtil1("D:/学习/00dw.csv");
List tt = cu.getList();
for (Iterator itt = tt.iterator(); itt.hasNext();)
System.out.println(itt.next().toString()+"||");
// System.out.println(cu.getRowNum());
// System.out.println(cu.getColNum());
// System.out.println(cu.getRow(0));
// System.out.println(cu.getCol(0));
// System.out.println(cu.getString(0, 0));
cu.CsvClose();
public void createCsvTest1(HttpServletResponse Response) throws IOException
CsvUtil1 cu = new CsvUtil1("D:/学习/00dw.csv");
List tt = cu.getList();
String data = "";
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmm");
Date today = new Date();
String dateToday = dataFormat.format(today);
File file=new File("D:/学习/001dw.csv");
if(!file.exists())
file.createNewFile();
// else
// file.delete() ;
String str[] ;
StringBuilder sb = new StringBuilder("");
BufferedWriter output=new BufferedWriter(new FileWriter(file,true));
for (Iterator itt = tt.iterator(); itt.hasNext();)
String fileStr = itt.next().toString() ;
str = fileStr.split(",");
for(int i=0;i<=str.length-1;i++) //拆分成数组 用于插入数据库中
System.out.print("str["+i+"]="+str[i]+" ");
System.out.println("");
sb.append(fileStr+"\r\n") ;
//System.out.println(sb.toString());
output.write(sb.toString());
output.flush() ;
output.close();
cu.CsvClose();
public static void main(String[] args) throws IOException
CsvUtil1 test = new CsvUtil1();
//test.test();
HttpServletResponse response = null ;
test.createCsvTest1(response);
参考技术A 可以通过流的形式读取到所有内容,之后在转换成元素的形式进行实现。举例:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
public class Test
public static void main(String[] args)
Hashtable<String, String[]> dict = new Hashtable<String, String[]>();
try
BufferedReader reader = new BufferedReader(new FileReader("test.csv"));
String line = null;
while((line=reader.readLine())!=null)
String item[] = line.split(",");
String item2[] = new String[19];
System.arraycopy(item,1,item2,0,19);
dict.put(item[0],item2);
Enumeration e2 = dict.keys();
while (e2.hasMoreElements())
String key = (String) e2.nextElement();
System.out.println(key);
String[] dd = (String[])dict.get(key);
for (int i=0;i<dd.length;i++)
System.out.print(dd[i]+"\t");
System.out.println();
catch (Exception e)
e.printStackTrace();
参考技术B 那你可以自己处理吧,比如判断是否存在双引号,如果存在的话,通过subString()方法,对字符串进行截取吧。
以上是关于android怎样读文本文件的内容的主要内容,如果未能解决你的问题,请参考以下文章
使用java的输入,输出流将一个文本文件的内容按行读出,每读一行就顺序添加行号,并写入到另一个文件