一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java 读取本地文件实例

时间:2013-09-10 编辑:简简单单 来源:一聚教程网


例1

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

 代码如下 复制代码

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

 代码如下 复制代码

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-

热门栏目