java讀取txt文件內(nèi)容。可以作如下理解:
舟曲網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、成都響應式網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)成立于2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)。
首先獲得一個文件句柄。File file = new File(); file即為文件句柄。兩人之間連通電話網(wǎng)絡了。接下來可以開始打電話了。
通過這條線路讀取甲方的信息:new FileInputStream(file) 目前這個信息已經(jīng)讀進來內(nèi)存當中了。接下來需要解讀成乙方可以理解的東西
既然你使用了FileInputStream()。那么對應的需要使用InputStreamReader()這個方法進行解讀剛才裝進來內(nèi)存當中的數(shù)據(jù)
解讀完成后要輸出呀。那當然要轉(zhuǎn)換成IO可以識別的數(shù)據(jù)呀。那就需要調(diào)用字節(jié)碼讀取的方法BufferedReader()。同時使用bufferedReader()的readline()方法讀取txt文件中的每一行數(shù)據(jù)哈。
package?com.campu;
import?java.io.BufferedInputStream;
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.InputStreamReader;
import?java.io.Reader;
/**
*?@author?Java團長
*?H20121012.java
*?2017-10-29上午11:22:21
*/
public?class?H20121012?{
/**
*?功能:Java讀取txt文件的內(nèi)容
*?步驟:1:先獲得文件句柄
*?2:獲得文件句柄當做是輸入一個字節(jié)碼流,需要對這個輸入流進行讀取
*?3:讀取到輸入流后,需要讀取生成字節(jié)流
*?4:一行一行的輸出。readline()。
*?備注:需要考慮的是異常情況
*?@param?filePath
*/
public?static?void?readTxtFile(String?filePath){
try?{
String?encoding="GBK";
File?file=new?File(filePath);
if(file.isFile()??file.exists()){?//判斷文件是否存在
InputStreamReader?read?=?new?InputStreamReader(
new?FileInputStream(file),encoding);//考慮到編碼格式
BufferedReader?bufferedReader?=?new?BufferedReader(read);
String?lineTxt?=?null;
while((lineTxt?=?bufferedReader.readLine())?!=?null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
}?catch?(Exception?e)?{
System.out.println("讀取文件內(nèi)容出錯");
e.printStackTrace();
}
}
public?static?void?main(String?argv[]){
String?filePath?=?"L:\\Apache\\htdocs\\res\\20121012.txt";
//??????"res/";
readTxtFile(filePath);
}
}
我有一個微信公眾號,經(jīng)常會分享一些Java技術相關的干貨文章,還有一些學習資源。
如果你需要的話,可以用微信搜索“Java團長”或者“javatuanzhang”關注。
本例使用java來讀取excel的內(nèi)容并展出出結(jié)果,代碼如下:
復制代碼 代碼如下:
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;
}
一.讀取xml配置文件
(一)新建一個java bean(HelloBean. java)
java代碼
(二)構造一個配置文件(beanConfig.xml)
xml 代碼
(三)讀取xml文件
1.利用ClassPathXmlApplicationContext
java代碼
2.利用FileSystemResource讀取
java代碼
二.讀取properties配置文件
這里介紹兩種技術:利用spring讀取properties 文件和利用java.util.Properties讀取
(一)利用spring讀取properties 文件
我們還利用上面的HelloBean. java文件,構造如下beanConfig.properties文件:
properties 代碼
helloBean.class=chb.demo.vo.HelloBean
helloBean.helloWorld=Hello!chb!
屬性文件中的"helloBean"名稱即是Bean的別名設定,.class用于指定類來源。
然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader來讀取屬性文件
java代碼
(二)利用java.util.Properties讀取屬性文件
比如,我們構造一個ipConfig.properties來保存服務器ip地址和端口,如:
properties 代碼
ip=192.168.0.1
port=8080
三.讀取位于Jar包之外的properties配置文件
下面僅僅是列出讀取文件的過程,剩下的解析成為properties的方法同上
1 FileInputStream reader = new FileInputStream("config.properties");
2 num = reader.read(byteStream);
3 ByteArrayInputStream inStream = new ByteArrayInputStream(byteStream, 0, num);
四.要讀取的配置文件和類文件一起打包到一個Jar中
String currentJarPath = URLDecoder.decode(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); //獲取當前Jar文件名,并對其解碼,防止出現(xiàn)中文亂碼
JarFile currentJar = new JarFile(currentJarPath);
JarEntry dbEntry = currentJar.getJarEntry("包名/配置文件");
InputStream in = currentJar.getInputStream(dbEntry);
//以上YourClassName是class全名,也就是包括包名
修改:
JarOutputStream out = new FileOutputStream(currentJarPath);
out.putNextEntry(dbEntry);
out.write(byte[] b, int off, int len); //寫配置文件
。。。
out.close();
Java讀取二進制文件,以字節(jié)為單位進行讀取,還可讀取圖片、音樂文件、視頻文件等,
在Java中,提供了四種類來對文件進行操作,分別是InputStream OutputStream Reader Writer ,前兩種是對字節(jié)流的操作,后兩種則是對字符流的操作。
示例代碼如下:
public static void readFileByBytes(String fileName){
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("一次讀一個");
// 一次讀一個字節(jié)
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
名稱欄目:java代碼實現(xiàn)讀取文件 讀取文件Java
網(wǎng)頁路徑:http://www.yijiale78.com/article48/hhigep.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、App開發(fā)、面包屑導航、外貿(mào)網(wǎng)站建設、用戶體驗、營銷型網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)