用Scanner讀取文件流,將會遍歷文件中的所有行——允許對每一行進行處理,而不保持對它的引用。總之沒有把它們存放在內(nèi)存中,這是java提供的一種針對大文件進行讀取的機制。
黑龍江網(wǎng)站建設公司創(chuàng)新互聯(lián),黑龍江網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為黑龍江近千家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的黑龍江做網(wǎng)站的公司定做!
以下是示例代碼:
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream(path);
sc = new Scanner(inputStream, "UTF-8");
while (sc.hasNextLine()) {
String line = sc.nextLine();
// System.out.println(line);
}
// note that Scanner suppresses exceptions
if (sc.ioException() != null) {
throw sc.ioException();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}
給一個代碼你就知道了,代碼里面文件的路徑改成你電腦上的文件的路徑
import?java.io.BufferedReader;
import?java.io.FileInputStream;
import?java.io.IOException;
import?java.io.InputStreamReader;
/**
*?file?IO流讀取并輸出文件
*?@author?Administrator
*
*/
public?class?FileIO?{
public?static?void?main(String[]?args)?throws?IOException?{
FileInputStream?fis?=?new?FileInputStream("src/day03/BrDemo.java");//?要讀的文件路徑
InputStreamReader?isr?=?new?InputStreamReader(fis);//?字符流
BufferedReader?br?=?new?BufferedReader(isr);?//?緩沖
String?line?=?null;
while?((line?=?br.readLine())?!=?null)?{//?字符不等于空
System.out.println(line);//?一行一行地輸出
}
br.close();//?關閉文件
}
}
本例使用java來讀取excel的內(nèi)容并展出出結果,代碼如下:
復制代碼 代碼如下:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ExcelOperate {
public static void main(String[] args) throws Exception {
File file = new File("ExcelDemo.xls");
String[][] result = getData(file, 1);
int rowLength = result.length;
for(int i=0;irowLength;i++) {
for(int j=0;jresult[i].length;j++) {
System.out.print(result[i][j]+"\t\t");
}
System.out.println();
}
}
/**
* 讀取Excel的內(nèi)容,第一維數(shù)組存儲的是一行中格列的值,二維數(shù)組存儲的是多少個行
* @param file 讀取數(shù)據(jù)的源Excel
* @param ignoreRows 讀取數(shù)據(jù)忽略的行數(shù),比喻行頭不需要讀入 忽略的行數(shù)為1
* @return 讀出的Excel中數(shù)據(jù)的內(nèi)容
* @throws FileNotFoundException
* @throws IOException
*/
public static String[][] getData(File file, int ignoreRows)
throws FileNotFoundException, IOException {
ListString[] result = new ArrayListString[]();
int rowSize = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
file));
// 打開HSSFWorkbook
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell cell = null;
for (int sheetIndex = 0; sheetIndex wb.getNumberOfSheets(); sheetIndex++) {
HSSFSheet st = wb.getSheetAt(sheetIndex);
// 第一行為標題,不取
for (int rowIndex = ignoreRows; rowIndex = st.getLastRowNum(); rowIndex++) {
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize rowSize) {
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
boolean hasValue = false;
for (short columnIndex = 0; columnIndex = row.getLastCellNum(); columnIndex++) {
String value = "";
cell = row.getCell(columnIndex);
if (cell != null) {
// 注意:一定要設成這個,否則可能會出現(xiàn)亂碼
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd")
.format(date);
} else {
value = "";
}
} else {
value = new DecimalFormat("0").format(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 導入時如果為公式生成的數(shù)據(jù)則無值
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_ERROR:
value = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y"
: "N");
break;
default:
value = "";
}
}
if (columnIndex == 0 value.trim().equals("")) {
break;
}
values[columnIndex] = rightTrim(value);
hasValue = true;
}