java代碼任意文件讀取 文件讀寫java代碼

用JAVA讀取文件信息

第一個問題的實現(xiàn):import java.io.File;public class Test { public static void main(String[] args) {

10年積累的成都網(wǎng)站制作、成都網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先制作網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有芮城免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

String filePath = "指定的文件夾地址";

File dir = new File(filePath);

if(!dir.isDirectory()){

System.out.println(filePath + " -- 這不是一個文件夾");

}else{

File[] fileList = dir.listFiles();

printFileList(fileList);

}

} private static void printFileList(File[] fileList) {

for(int i = 0;i fileList.length;i++){

if(fileList[i].isFile()){

System.out.println("文件名字.文件類型: " + fileList[i].getAbsolutePath() + fileList[i].getName());

System.out.println("文件大小: " + fileList[i].length() + " 字節(jié)");

}else if(fileList[i].isDirectory()){

File[] newFileList = fileList[i].listFiles();

printFileList(newFileList);

}

}

}}

JAVA開發(fā)讀取文件的方法有哪些

/**

* 根據(jù)提供地址讀取文件返回字符串

* @param filePath

* @return 文件字符串

*/

private String readFile(String filePath){

File javaFile = new File(filePath);

StringBuffer fileStr = new StringBuffer();//存儲杜滸的文件字符串,.

int b;

InputStream fileIns = null;

InputStreamReader fileReder = null;

try {

fileIns = new FileInputStream(javaFile);

fileReder = new InputStreamReader(fileIns, "utf-8");

while ((b = fileReder.read()) != -1) {

fileStr.append((char) b);

}

// System.out.println(javaStr.toString());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (fileReder != null) {

fileReder.close();

}

if (fileIns != null) {

fileIns.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

if(fileStr.length()0){

return fileStr.toString();

}else{

return "";

}

// System.out.println(result);

}

僅供參考!

java文件讀取

String fileName = "tst.xml";

try {

BufferedReader bf = new BufferedReader(new FileReader(fileName));

ListString contents = new ArrayListString();

String content = null;

while((content = bf.readLine()) != null){

if(!content.trim().equals("")){

contents.add(content);

}

}

bf.close();

} catch (IOException e) {

e.printStackTrace();

}

名稱欄目:java代碼任意文件讀取 文件讀寫java代碼
標(biāo)題網(wǎng)址:http://muchs.cn/article20/docopco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、Google、品牌網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、定制開發(fā)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作