Java reads local file instances

Source: Internet
Author: User

To read files in java, we need to use java. io. file, java. io. fileInputStream, java. io. inputStreamReader has waited for the library file. Now I have found two examples for java to read local files.


Example 1

Read the local example and find the corresponding jar package if an error occurs!

The Code is as follows: Copy code

Package loginQQ;

 


Import java. io. BufferedReader;

Import java. io. File;

Import java. io. FileInputStream;

Import java. io. InputStreamReader;

 


Public class LoadingRead {

 


/**

* Read the configuration file

*/

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 () {// checks whether a file exists.

// Avoid Chinese character encoding Problems

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 ("sorry, The INI file is not found ");

}

} Catch (Exception e ){

E. printStackTrace ();

}

}

}

Example 2

The Code is as follows: Copy code

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 ());
}
 
}
 
}
 
}

Output:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.