給一個(gè)代碼你就知道了,代碼里面文件的路徑改成你電腦上的文件的路徑
創(chuàng)新互聯(lián)公司2013年成立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元平泉做網(wǎng)站,已為上家服務(wù),為平泉各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
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();//?關(guān)閉文件
}
}
用Scanner讀取文件流,將會(huì)遍歷文件中的所有行——允許對(duì)每一行進(jìn)行處理,而不保持對(duì)它的引用。總之沒(méi)有把它們存放在內(nèi)存中,這是java提供的一種針對(duì)大文件進(jìn)行讀取的機(jī)制。
以下是示例代碼:
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();
}
}
本例使用java來(lái)讀取excel的內(nèi)容并展出出結(jié)果,代碼如下:
復(fù)制代碼 代碼如下:
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ù)組存儲(chǔ)的是一行中格列的值,二維數(shù)組存儲(chǔ)的是多少個(gè)行
* @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));
// 打開(kāi)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);
// 第一行為標(biāo)題,不取
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) {
// 注意:一定要設(shè)成這個(gè),否則可能會(huì)出現(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:
// 導(dǎo)入時(shí)如果為公式生成的數(shù)據(jù)則無(wú)值
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;
}
有關(guān)Java如何讀寫(xiě)txt文件這個(gè)問(wèn)題經(jīng)常在面試時(shí)會(huì)被問(wèn)到,不懂或不熟悉的同志們可是要記好了喲!先來(lái)看下具體實(shí)現(xiàn)吧! package common; import java.io.*; import java.util.ArrayList; public class IOTest { public static void main (String args[]) { ReadDate(); WriteDate(); } /** * 讀取數(shù)據(jù) */ public static void ReadDate() { String url = “e:/2.txt”; try { FileReader read = new FileReader(new File(url)); StringBuffer sb = new StringBuffer(); char ch[] = new char[1024]; int d = read.read(ch); while(d!=-1){ String str = new String(ch,0,d); sb.append(str); d = read.read(ch); } System.out.print(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 寫(xiě)入數(shù)據(jù) */ public static void WriteDate() { try{ File file = new File(“D:/abc.txt”); if (file.exists()) { file.delete(); } file.createNewFile(); BufferedWriter output = new BufferedWriter(new FileWriter(file)); ArrayList ResolveList = new ArrayList(); for (int i = 0; i 10; i++) { ResolveList.add(Math.random()* 100); } for (int i=0 ;i output.write(String.valueOf(ResolveList.get(i)) + “\n”); } output.close(); } catch (Exception ex) { System.out.println(ex); } } }
package file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class ReadFile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//將一個(gè)已存在文件加載到內(nèi)存中,讀取文本文件
InputStream is = new FileInputStream("xxx.txt");
//獲得文件可讀取的字節(jié)數(shù)
int num = is.available();
System.out.println(num);
char[] cs = new char[num];
for (int i = 0; i num; i++) {
int num2 = is.read();//ashc碼
char c = (char)num2;//ashc轉(zhuǎn)換成字符
cs[i]=c;
}
String str = new String(cs);
System.out.println(str);
is.close();
}
}