Hadoop writes, deletes, and reads files to HDFs

Source: Internet
Author: User

First talk about some of the problems encountered:

1, such as the configuration of this if the point Eclipse Red Fork,. Import is the wrong thing to be imported .... There are several classes or methods that can present this problem.

2, with a few online code, inexplicably wrong, and then read their own book combined with the online code to change the following version

3, byte[] array opened relatively large and then write, there will be a problem is written a lot of 0, my method is more stupid, to string judgment, for 0 after the do not write.

I realized the function is to read the contents of the file, delete, in creating a file with the same name to write new things

Import Java.net.uri;import java.util.arrays;import Java.io.*;import Org.apache.hadoop.io.*;import Org.apache.hadoop.conf.configuration;import Org.apache.hadoop.fs.*;import Org.apache.hadoop.fs.filesystem;public Class Filesystemcat {public static void main (string[] args) throws ioexception{string uri = Args[0]; String uro = args[0]; Configuration conf = new configuration (), try {FileSystem fs = Filesystem.get (Uri.create (URI), conf);//filesystem HDFs = Fi Lesystem.get (Uri.create (Uro), conf); Fsdatainputstream in = Fs.open (new Path (URI));//ioutils.copybytes (in, System.out,4096,false);//in.seek (1);//          Ioutils.copybytes (in, system.out,4096, false);//file read byte[] Iobuffer = new byte[1024];              int readlen = In.read (Iobuffer);          while (readlen!=-1) {Readlen = In.read (Iobuffer);            } String str = new string (Iobuffer);        int cnt=0;        while ((int) (Str.charat (CNT))! = 0) cnt++;        Str=str.substring (0, CNT); System.out.println (str);          In.close ();       Deletion of the file Fs.delete (new Path (URI), true);        File writes Fsdataoutputstream out = Fs.create (new Path (Uro));        Out.write (("New1" +str). GetBytes ("UTF-8"));        Out.flush ();        Out.sync ();        Out.close (); Fs.close ();} catch (Exception e) {e.printstacktrace ();}}}


Hadoop writes, deletes, and reads files to HDFs

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.