java 讀取本地檔案執行個體

來源:互聯網
上載者:User


例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-

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.