资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java代码任意文件读取 文件读写java代码

用JAVA读取文件信息

第一个问题的实现:import java.io.File;public class Test { public static void main(String[] args) {

10年积累的成都网站制作、成都网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先制作网站后付款的网站建设流程,更有芮城免费网站建设让你可以放心的选择与我们合作。

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() + " 字节");

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

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

printFileList(newFileList);

}

}

}}

JAVA开发读取文件的方法有哪些

/**

* 根据提供地址读取文件返回字符串

* @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代码
链接地址:http://cdkjz.cn/article/docopco.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220