java读取文件的所有内容|java如何读取整个excel文件的内容

java读取文件的所有内容|java如何读取整个excel文件的内容的第1张示图

1. java如何读取整个excel文件的内容

在Java中读取Excel文件的内容

在这里,我使用的是一个叫Java Excel API的东西,类似的还有的POI,不过感觉那个

太复杂了点儿。而且jxl对中文的支持相当的好,至少我在用的过程中一点问题没出。

一、下载地址

http://www.andykhan.com/jexcelapi/

二、特性

可以读取Excel 95, 97, 2000文件

可以读或写Excel 97及其以后版本的的公式(不过我发现好像有bug)

生成Excel 97格式的电子表格

支持字体、数字和日期格式化

支持单元格的颜色和阴影

可以编辑现有的文件

三、读文件

//声明一下,记得后面要关闭哦。。

Workbook workbook = null;

try {

workbook = Workbook.getWorkbook(new File("d:\temp\TestRead.xls"));

} catch (Exception e) {

throw new Exception("file to import not found!");

}

Sheet sheet = workbook.getSheet(0);

Cell cell = null;

int columnCount=3;

int rowCount=sheet.getRows();

for (int i = 0; i<rowcount; p="" {

for (int j = 0; j<columncount; p="" {

//注意,这里的两个参数,第一个是表示列的,第二才表示行

cell=sheet.getCell(j, i);

//要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确

if(cell.getType()==CellType.NUMBER){

System.out.print(((NumberCell)cell).getValue());

}

else if(cell.getType()==CellType.DATE){

System.out.print(((DateCell)cell).getDate());

}

else{

System.out.print(cell.getContents());

}

//System.out.print(cell.getContents());

System.out.print("");

}

System.out.print("");

}

//关闭它,否则会有内存泄露

workbook.close();

2. java如何读取一个txt文件的所有内容

import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class ReadFile { public static void main(String[] args) throws IOException { String fileContent = readFileContent(""); System.out.println(fileContent); } //参数string为你的文件名 private static String readFileContent(String fileName) throws IOException { File file = new File(fileName); BufferedReader bf = new BufferedReader(new FileReader(file)); String content = ""; StringBuilder sb = new StringBuilder(); while(content != null){ content = bf.readLine(); if(content == null){ break; } sb.append(content.trim()); } bf.close(); return sb.toString(); }}求采纳为满意回答。

3. java怎么循环读取目录下的文件内容

JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)1.不使用递归的方法调用。public void traverseFolder1(String path) {int fileNum = 0, folderNum = 0;File file = new File(path);if (file.exists()) {LinkedList<File> list = new LinkedList<File>();File[] files = file.listFiles();for (File file2 : files) {if (file2.isDirectory()) {System.out.println("文件夹:" + file2.getAbsolutePath());list.add(file2);fileNum++;} else {System.out.println("文件:" + file2.getAbsolutePath());folderNum++;}}File temp_file;while (!list.isEmpty()) {temp_file = list.removeFirst();files = temp_file.listFiles();for (File file2 : files) {if (file2.isDirectory()) {System.out.println("文件夹:" + file2.getAbsolutePath());list.add(file2);fileNum++;} else {System.out.println("文件:" + file2.getAbsolutePath());folderNum++;}}}} else {System.out.println("文件不存在!");}System.out.println("文件夹共有:" + folderNum + ",文件共有:" + fileNum);}2.使用递归的方法调用public static List<File> getFileList(String strPath) {File dir = new File(strPath);File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组if (files != null) {for (int i = 0; i < files.length; i++) {String fileName = files[i].getName();if (files[i].isDirectory()) { // 判断是文件还是文件夹getFileList(files[i].getAbsolutePath()); // 获取文件绝对路径} else if (fileName.endsWith("avi")) { // 判断文件名是否以.avi结尾String strFileName = files[i].getAbsolutePath();System.out.println("—" + strFileName);filelist.add(files[i]);} else {continue;}}}return filelist;}

4. java中如何从文件中读取数据

分为读字节,读字符两种读法◎◎◎FileInputStream 字节输入流读文件◎◎◎public class Maintest {public static void main(String[] args) throws IOException {File f=new File("G:\\just for fun\\xiangwei.txt");FileInputStream fin=new FileInputStream(f);byte[] bs=new byte[1024];int count=0;while((count=fin.read(bs))>0){String str=new String(bs,0,count);//反复定义新变量:每一次都 重新定义新变量,接收新读取的数据System.out.println(str);//反复输出新变量:每一次都 输出重新定义的新变量}fin.close();}}◎◎◎FileReader 字符输入流读文件◎◎◎public class Maintest {public static void main(String[] args) throws IOException {File f=new File("H:\\just for fun\\xiangwei.txt");FileReader fre=new FileReader(f);BufferedReader bre=new BufferedReader(fre);String str="";while((str=bre.readLine())!=null)//●判断最后一行不存在,为空{System.out.println(str);}bre.close(); fre.close();}}

5. Java怎么读取目录下的所有文件

这个要求比较苛刻emmm…给你个思路吧,如果你不想写的话可以追问我,因为我没有时间,所以就不写了先用File类实例该文件夹,然后读取其目录下的所有文件,如果是文件获取它的文件名字,判断是否以.txt结尾,如果是的话用正则表达式匹配,将匹配的部分(把正则给你吧,[^\w\.]* )进行删除,更改该文件名字(如果在其他文件夹生成就很麻烦,所以先复制一份,再操纵那一份重命名也不影响对吧(皮))

6. Java如何实现读取一个txt文件的所有内容,然后提取所需的部分并把它写入到另一个txt文件中

代码如下:

importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.util.ArrayList;importjava.util.List;publicclassApp{/***保存list到指定文件*@paramlist*@paramfilePath*@throwsIOException*@throwsFileNotFoundException*/staticvoidsave(List<String>list,StringfilePath)throwsFileNotFoundException,IOException{try(FileOutputStreamoutputStream=newFileOutputStream(filePath);=newOutputStreamWriter(outputStream);BufferedWriterwriter=newBufferedWriter(streamWriter)){for(Stringline:list){writer.write(line+System.lineSeparator());}}}publicstaticvoidmain(String[]args)throwsIOException{List<String>list1=newArrayList<>();List<String>list2=newArrayList<>();List<String>list3=newArrayList<>();Filefile=newFile("d:/temp/0.txt");try(FileInputStreamstream=newFileInputStream(file);InputStreamReaderstreamReader=newInputStreamReader(stream);BufferedReaderreader=newBufferedReader(streamReader)){Stringline="";while((line=reader.readLine())!=null){//去除开始的数字line=line.replaceFirst("\d+","");if(line.contains("Organ")){//包含Organ放入list1list1.add(line);}elseif(line.contains("Location")){//包含Location放入list2list2.add(line);}elseif(line.contains("Person")){//包含Person放入list3list3.add(line);}}}if(!list1.isEmpty()){save(list1,"d:/temp/1.txt");}if(!list2.isEmpty()){save(list2,"d:/temp/2.txt");}if(!list3.isEmpty()){save(list3,"d:/temp/3.txt");}}}

7. JAVA中读取文件(二进制,字符)内容的几种方

JAVA中读取文件内容的方法有很多,比如按字节读取文件内容,按字符读取文件内容,按行读取文件内容,随机读取文件内容等方法,本文就以上方法的具体实现给出代码,需要的可以直接复制使用public class ReadFromFile {/*** 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。*/public static void readFileByBytes(String fileName) {File file = new File(fileName);InputStream in = null;try {System.out.println("以字节为单位读取文件内容,一次读一个字节:");// 一次读一个字节in = new FileInputStream(file);int tempbyte;while ((tempbyte = in.read()) != -1) {System.out.write(tempbyte);}in.close();} catch (IOException e) {e.printStackTrace();return;}try {System.out.println("以字节为单位读取文件内容,一次读多个字节:");// 一次读多个字节byte[] tempbytes = new byte[100];int byteread = 0;in = new FileInputStream(fileName);ReadFromFile.showAvailableBytes(in);// 读入多个字节到字节数组中,byteread为一次读入的字节数while ((byteread = in.read(tempbytes)) != -1) {System.out.write(tempbytes, 0, byteread);}} catch (Exception e1) {e1.printStackTrace();} finally {if (in != null) {try {in.close();} catch (IOException e1) {}}}}/*** 以字符为单位读取文件,常用于读文本,数字等类型的文件*/public static void readFileByChars(String fileName) {File file = new File(fileName);Reader reader = null;try {System.out.println("以字符为单位读取文件内容,一次读一个字节:");// 一次读一个字符reader = new InputStreamReader(new FileInputStream(file));int tempchar;while ((tempchar = reader.read()) != -1) {// 对于windows下,\r\n这两个字符在一起时,表示一个换行。// 但如果这两个字符分开显示时,会换两次行。// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。if (((char) tempchar) != '\r') {System.out.print((char) tempchar);}}reader.close();} catch (Exception e) {e.printStackTrace();}try {System.out.println("以字符为单位读取文件内容,一次读多个字节:");// 一次读多个字符char[] tempchars = new char[30];int charread = 0;reader = new InputStreamReader(new FileInputStream(fileName));// 读入多个字符到字符数组中,charread为一次读取字符数while ((charread = reader.read(tempchars)) != -1) {// 同样屏蔽掉\r不显示if ((charread == tempchars.length)&& (tempchars[tempchars.length – 1] != '\r')) {System.out.print(tempchars);} else {for (int i = 0; i < charread; i++) {if (tempchars[i] == '\r') {continue;} else {System.out.print(tempchars[i]);}}}}} catch (Exception e1) {e1.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {}}}}/*** 以行为单位读取文件,常用于读面向行的格式化文件*/public static void readFileByLines(String fileName) {File file = new File(fileName);BufferedReader reader = null;try {System.out.println("以行为单位读取文件内容,一次读一整行:");reader = new BufferedReader(new FileReader(file));String tempString = null;int line = 1;// 一次读入一行,直到读入null为文件结束while ((tempString = reader.readLine()) != null) {// 显示行号System.out.println("line " + line + ": " + tempString);line++;}reader.close();} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {}}}}/*** 随机读取文件内容*/public static void readFileByRandomAccess(String fileName) {RandomAccessFile randomFile = null;try {System.out.println("随机读取一段文件内容:");// 打开一个随机访问文件流,按只读方式randomFile = new RandomAccessFile(fileName, "r");// 文件长度,字节数long fileLength = randomFile.length();// 读文件的起始位置int beginIndex = (fileLength > 4) ? 4 : 0;// 将读文件的开始位置移到beginIndex位置。randomFile.seek(beginIndex);byte[] bytes = new byte[10];int byteread = 0;// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。// 将一次读取的字节数赋给bytereadwhile ((byteread = randomFile.read(bytes)) != -1) {System.out.write(bytes, 0, byteread);}} catch (IOException e) {e.printStackTrace();} finally {if (randomFile != null) {try {randomFile.close();} catch (IOException e1) {}}}}/*** 显示输入流中还剩的字节数*/private static void showAvailableBytes(InputStream in) {try {System.out.println("当前字节输入流中的字节数为:" + in.available());} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {String fileName = "C:/temp/newTemp.txt";ReadFromFile.readFileByBytes(fileName);ReadFromFile.readFileByChars(fileName);ReadFromFile.readFileByLines(fileName);ReadFromFile.readFileByRandomAccess(fileName);}}

8. 如何用java读取txt文件的内容

import java.io.*; public class Read{ public static void main(String [] args) { try{ FileReader f = new FileReader("D:\\1.txt"); // 注意路径这里可以有两种表示方法一个种\\回一种/就可以了答 BufferedReader buf = new BufferedReader(f); String s; while((s = buf.readLine() ) != null){ System.out.println(s); } f.close(); buf.close(); }catch(IOException e){} }}

9. java如何读取整个excel文件的内容

在Java中读取Excel文件的内容

在这里,我使用的是一个叫Java Excel API的东西,类似的还有jakarta的POI,不过感觉那个

太复杂了点儿。而且jxl对中文的支持相当的好,至少我在用的过程中一点问题没出。

一、下载地址

http://www.andykhan.com/jexcelapi/

二、特性

可以读取Excel 95, 97, 2000文件

可以读或写Excel 97及其以后版本的的公式(不过我发现好像有bug)

生成Excel 97格式的电子表格

支持字体、数字和日期格式化

支持单元格的颜色和阴影

可以编辑现有的文件

三、读文件

//声明一下,记得后面要关闭哦。。

Workbook workbook = null;

try {

workbook = Workbook.getWorkbook(new File("d:\temp\TestRead.xls"));

} catch (Exception e) {

throw new Exception("file to import not found!");

}

Sheet sheet = workbook.getSheet(0);

Cell cell = null;

int columnCount=3;

int rowCount=sheet.getRows();

for (int i = 0; i<rowcount; p="" {

for (int j = 0; j<columncount; p="" {

//注意,这里的两个参数,第一个是表示列的,第二才表示行

cell=sheet.getCell(j, i);

//要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确

if(cell.getType()==CellType.NUMBER){

System.out.print(((NumberCell)cell).getValue());

}

else if(cell.getType()==CellType.DATE){

System.out.print(((DateCell)cell).getDate());

}

else{

System.out.print(cell.getContents());

}

//System.out.print(cell.getContents());

System.out.print("");

}

System.out.print("");

}

//关闭它,否则会有内存泄露

workbook.close();

10. java怎样读取文件所有内容,主要是跳行问题谢谢了

如果是字符流文件。可以使用java.nio.file.Files类的readAllLines将所有内容读到一个List<String>里专Google Guava库也提供了类属似的功能com.google.common.io.Filesstatic StringtoString(File file, Charset charset) Reads all characters from a file into a String, using the given character set.

未经允许不得转载:山九号 » java读取文件的所有内容|java如何读取整个excel文件的内容

赞 (0)