教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 java 读取本地文件实例

java 读取本地文件实例

发布时间:2016-11-30   编辑:jiaochengji.com
教程集为您提供java 读取本地文件实例等资源,欢迎您收藏本站,我们将为您提供最新的java 读取本地文件实例资源
在java中读取文件我们会要用到java.io.File,java.io.FileInputStream,java.io.InputStreamReader等一下库文件了,下面我找了两个java读取本地文件的实例,各位朋友可参考。


例1

读取本地例子,出现错误自己去寻找相应的jar包!

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5764')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5764>

package loginQQ;

 


import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

 


public class LoadingRead {

 


/**

* 读取配置文件

*/

public static void main(String[] args) {

String endcode = "GBK";

File file = new File("D:/Java/YT/src/com/YT/auto/word.ini");

InputStreamReader in = null;

StringBuffer pzFile = new StringBuffer();

try{

if(file.isFile() && file.exists()){//判断是否有文件

//避免汉字编码问题

in = new InputStreamReader(new FileInputStream(file) , endcode);

BufferedReader buffer = new BufferedReader(in);

String lineText = null;

while((lineText = buffer.readLine()) != null){

pzFile.append(lineText);

}

System.out.println(pzFile);

}else{

System.out.println("抱歉哦,没有找到ini文件");

}

}catch (Exception e) {

e.printStackTrace();

}

}

}

例2

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7530')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7530>

package my.test;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
 
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
 *
 * @author zhouhh
 * @date 2013.3.14
 * merge files to hdfs
 */
public class testhdfspaths {
 public static void main(String[] args) throws IOException {
  String sourceFile = "";
  String targetFile = "";
  if (args.length < 2) {
   sourceFile = "/home/zhouhh/test";
   targetFile = "hdfs://hadoop48:54310/user/zhouhh";
  } else {
   sourceFile = args[0];
   targetFile = args[1];
  }
  Configuration conf = new Configuration();
  FileSystem hdfs = FileSystem.get(conf);
  FileSystem local = FileSystem.getLocal(conf);
 
  try {
   visitPath(local,sourceFile);
   visitPath(hdfs,targetFile);
  } catch (IOException e) {
   e.printStackTrace();
  }
 
 }
 
 public static void visitPath(FileSystem fs,String path) throws IOException
 {
  Path inputDir = new Path(path);
  FileStatus[] inputFiles = fs.listStatus(inputDir);
  if(inputFiles==null)
  {
   throw new IOException(" the path is not correct:" path);
  }
 
  System.out.println("----------------path:" path "----------------");
  for (int i = 0; i < inputFiles.length; i ) {
 
 
 
   if(inputFiles[i].isDir())
   {
    System.out.println(inputFiles[i].getPath().getName() " -dir-");
    visitPath(fs,inputFiles[i].getPath().toString());
   }
   else
   {
    System.out.println(inputFiles[i].getPath().getName() ",len:" inputFiles[i].getLen());
   }
 
  }
 
 }
 
}

输出:

----------------path:/home/zhouhh/test----------------
HelloWorldApp.class,len:432
a.scm,len:99
jar -dir-
----------------path:file:/home/zhouhh/test/jar----------------
Test.jar,len:1040
name.txt,len:389211
jdk-6u38-linux-amd64.rpm,len:58727459
hbase_thrift.tar.gz,len:770987
src -dir-
...
----------------path:hdfs://hadoop48:54310/user/zhouhh----------------
README.txt,len:1399
a,len:0
fsimage,len:9358
gz -dir-
----------------path:hdfs://hadoop48:54310/user/zhouhh/gz----------------
abs.gz,len:309589
mytest.txt,len:20
output -dir-

您可能感兴趣的文章:
Java的网络功能与编程 一
javascript基础教程(1)-语言特点
java 读取本地文件实例
Java入门笔记7_Stream
js读取与写入本地文件实现代码
js读写文件实例代码(读取与写入文本文件)
java 文件读写实例(读写csv文件)
在Java中获取系统属性
Linux操作系统的可执行文件格式详细解析
php 获取网站地址的函数代码

[关闭]
~ ~