2个回答
展开全部
android中读取的方式跟Java是一样的
String encoding="GBK";
try {
File file=new File(filepath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
int lineIndex=0;//判断第几行,readLine方法整行读取
while((lineTxt = bufferedReader.readLine()) != null){
lineIndex++;
if(lineIndex==5){
System.out.println(lineTxt);
}
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
String encoding="GBK";
try {
File file=new File(filepath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
int lineIndex=0;//判断第几行,readLine方法整行读取
while((lineTxt = bufferedReader.readLine()) != null){
lineIndex++;
if(lineIndex==5){
System.out.println(lineTxt);
}
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
展开全部
Android 读取txt文件规定的行数:
String encoding="GBK";
try {
File file=new File(filepath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
int lineIndex=0;//判断第几行,readLine方法整行读取
while((lineTxt = bufferedReader.readLine()) != null){
lineIndex++;
if(lineIndex==5){
System.out.println(lineTxt);
}
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
String encoding="GBK";
try {
File file=new File(filepath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
int lineIndex=0;//判断第几行,readLine方法整行读取
while((lineTxt = bufferedReader.readLine()) != null){
lineIndex++;
if(lineIndex==5){
System.out.println(lineTxt);
}
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询