“java”中多线程按行读取txt且每个线程读的内容不能重复,这么求“demo”?

“java”中多线程按行读取txt且每个线程读的内容不能重复,这么求“demo”?,第1张

public class Test{\x0d\x0a public static void main(String args[])throws Exception{\x0d\x0a File file = new File("D:\\Test.java")//Text文件\x0d\x0aBufferedReader br = new BufferedReader(new FileReader(file))//构造一个BufferedReader类来读取文件\x0d\x0aString s = null\x0d\x0awhile((s = br.readLine())!=null){//使用readLine方法,一次读一行\x0d\x0aSystem.out.println(s)\x0d\x0a}\x0d\x0abr.close()\x0d\x0a }\x0d\x0a}

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()

}

}

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/6049576.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-13
下一篇 2023-03-13

发表评论

登录后才能评论

评论列表(0条)

保存