java中如何调用txt里的数据

java中如何调用txt里的数据,第1张

要用输入流。

import javaio;

void readTxt(String txtpath){

File file = new File(txtPath); // 根据tx立文件路径,建了文件 D:\JAVA\OKTXT

FileReader fr = null;

bufferedReader br = null;

String str = null;

try{

fr = new FileReader(file); //文件输入流

br = new BufferedReader(fr); // 缓冲输入流

while((str = brreadLine())!=null){ //按行读取txt文件,存在str中

// 可以再这里对str进行 *** 作

}

}catch(IOException e){

Systemoutprintln(egetMessage());}

try{

frclose();

brclose();

}catch(IOException e){

Systemoutprintln(egetMessage());}

}

// 将文件中的数据以行为记录放入list

private List<String> getTxtList() throws IOException{

String line = null;

BufferedReader bufferedReader = null;

List<String> txtList = new ArrayList<String>();

try{

InputStream in = customerIdsCsvgetInputStream();

File file = new File(filepath);//filepath是文件路径名 就是前台传过来的

bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));// 此处是文件编码,根据实际可以变化

while ((line = bufferedReaderreadLine()) != null) {

String newLine = StringUtilstrim(line);

if(!newLineisEmpty()){

txtListadd(newLine);

}

}

bufferedReaderclose();

}catch(Exception e){

//异常处理

}

return txtList;

}

// 直接给一个只读一行的吧,呵呵,数字之间以空格来分隔

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

File file = new File("src/testtxt");

double[] a = getLineFromTxt(file," ");

for(int i = 0; i< alength; i++){

Systemoutprintln("a["+i+"] = "+a[i]);

}

}

test中的数据以空格隔开 12 32 3202

public static double[] getLineFromTxt(File file, String split) throws Exception{

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

String firstLine = brreadLine(); //就读第一行哦

String[] arrs = firstLinesplit(" ");

//将字符数组转为double数组

double[] arr = new double[arrslength];

for(int i = 0; i< arrlength; i++){

arr[i] = DoubleparseDouble(arrs[i]);

}

if(br!= null){

brclose();

br = null;

}

return arr;

}

以上就是关于java中如何调用txt里的数据全部的内容,包括:java中如何调用txt里的数据、java怎么获取txt文档,具体文件如下.求高手指导,最好贴源码供参考.先谢谢啦、java里怎么读取txt文件里的数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://www.54852.com/web/9431223.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存