「java以行读文件」java读文件可以一行一行读吗

博主:adminadmin 2022-11-27 11:42:06 73

本篇文章给大家谈谈java以行读文件,以及java读文件可以一行一行读吗对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java读txt方法

1).按行读取TXT文件

package zc;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class readLine {

public static void main(String[] args) {

// TODO Auto-generated method stub

File file = new File("C:/zc.txt");

BufferedReader reader = null;

String tempString = null;

int line =1;

try {

System.out.println("以行为单位读取文件内容,一次读一整行:");

reader = new BufferedReader(new FileReader(file));

while ((tempString = reader.readLine()) != null) {

System.out.println("Line"+ line + ":" +tempString);

line ++ ;

}

reader.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if(reader != null){

try {

reader.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

2).按字节读取TXT文件

package zc;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

public class readerFileByChars {

public static void main(String[] args) {

// TODO Auto-generated method stub

File file = new File("c:/zc.txt");

InputStream in = null;

byte[] tempByte = new byte[1024];

int byteread = 0;

try {

System.out.println("以字节为单位读取文件内容,一次读多个字节:");

in = new FileInputStream(file);

while ((byteread = in.read(tempByte)) != -1 ) {

System.out.write(tempByte, 0, byteread);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if (in != null) {

try {

in.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

JAVA实现txt文件按行读取时,怎么不读最后两行

知友你好: 你参看下面这段代码看看,这段代码是读数字的~

package test;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class Test {

public static double[] writeToDat(String path) {

File file = new File(path);

List list = new ArrayList();

double[] nums = null;

try {

BufferedReader bw = new BufferedReader(new FileReader(file));

String line = null;

//因为不知道有几行数据,所以先存入list集合中

while((line = bw.readLine()) != null){

list.add(line);

}

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

//确定数组长度

nums = new double[list.size()];

for(int i=0;ilist.size();i++){

String s = (String) list.get(i);

nums[i] = Double.parseDouble(s);

}

return nums;

}

public static void main(String[] args) {

String path = "d:/file4.txt";

double[] nums = writeToDat(path);

for(int i=0;inums.length;i++){

System.out.println(nums[i]);

}

}

}

java 读取行文件, 根据名字写入列文件

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Set;

//java 读取文件,一行一行的读取(1.txt)之后,我想把它的内容进行更改(2.txt的样子)写入 2.txt

//目前只是 按行读取了,但是不知道怎么 写入,2.txt的形式啊,请问有高手,帮忙解决下吗。。。

public class Test3 {

private static final String FILE_PATH = "1.txt";

private static final String FILE_PATH_OUT = "2.txt";

public static ListMapString,String readFileContent(String filepath){

BufferedReader br = null;

StringBuffer sb = new StringBuffer();

String line = "";

ListMapString,String list=new ArrayListMapString,String();

try {

br = new BufferedReader(new FileReader(new File(FILE_PATH)));

MapString,String map=new HashMapString,String();

while((line = br.readLine())!= null){

int index=line.indexOf(":");

if(index0)

continue;

String[] str=line.split(":");

if(map.containsKey(str[0])){

list.add(map);

map=new HashMapString,String();

}

map.put(str[0], str[1]);

}

list.add(map);

writeFileContent(FILE_PATH_OUT, list);

} catch (Exception e) {

System.err.println("文件读取错误!");

}finally{

try {

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return list;

}

public static void writeFileContent(String filepath,ListMapString,String list){

try {

BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filepath)));

String title="";

for(String s:list.get(0).keySet()){

title+=s+"\t";

}

title+="\n";

String value="";

for(MapString,String map:list){

for(String s:map.values()){

value+=s+"\t";

}

value+="\n";

}

bw.write(title);

bw.write(value);

bw.flush();

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) throws Exception {

//System.out.println(readFileContent(FILE_PATH));

readFileContent(FILE_PATH);

}

}

测试通过。

关于java以行读文件和java读文件可以一行一行读吗的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。