重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
java讀取txt文件內容。可以作如下理解:
舟曲網站制作公司哪家好,找創新互聯!從網頁設計、網站建設、微信開發、APP開發、成都響應式網站建設等網站項目制作,到程序開發,運營維護。創新互聯成立于2013年到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創新互聯。
首先獲得一個文件句柄。File file = new File(); file即為文件句柄。兩人之間連通電話網絡了。接下來可以開始打電話了。
通過這條線路讀取甲方的信息:new FileInputStream(file) 目前這個信息已經讀進來內存當中了。接下來需要解讀成乙方可以理解的東西
既然你使用了FileInputStream()。那么對應的需要使用InputStreamReader()這個方法進行解讀剛才裝進來內存當中的數據
解讀完成后要輸出呀。那當然要轉換成IO可以識別的數據呀。那就需要調用字節碼讀取的方法BufferedReader()。同時使用bufferedReader()的readline()方法讀取txt文件中的每一行數據哈。
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文件的內容
*?步驟:1:先獲得文件句柄
*?2:獲得文件句柄當做是輸入一個字節碼流,需要對這個輸入流進行讀取
*?3:讀取到輸入流后,需要讀取生成字節流
*?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("讀取文件內容出錯");
e.printStackTrace();
}
}
public?static?void?main(String?argv[]){
String?filePath?=?"L:\\Apache\\htdocs\\res\\20121012.txt";
//??????"res/";
readTxtFile(filePath);
}
}
我有一個微信公眾號,經常會分享一些Java技術相關的干貨文章,還有一些學習資源。
如果你需要的話,可以用微信搜索“Java團長”或者“javatuanzhang”關注。
本例使用java來讀取excel的內容并展出出結果,代碼如下:
復制代碼 代碼如下:
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的內容,第一維數組存儲的是一行中格列的值,二維數組存儲的是多少個行
* @param file 讀取數據的源Excel
* @param ignoreRows 讀取數據忽略的行數,比喻行頭不需要讀入 忽略的行數為1
* @return 讀出的Excel中數據的內容
* @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) {
// 注意:一定要設成這個,否則可能會出現亂碼
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:
// 導入時如果為公式生成的數據則無值
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文件名,并對其解碼,防止出現中文亂碼
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讀取二進制文件,以字節為單位進行讀取,還可讀取圖片、音樂文件、視頻文件等,
在Java中,提供了四種類來對文件進行操作,分別是InputStream OutputStream Reader Writer ,前兩種是對字節流的操作,后兩種則是對字符流的操作。
示例代碼如下:
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;
}