
1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.text.SimpleDateFormat; 4 import java.util.Scanner; 5 6 import org.apache.hadoop.conf.Configuration; 7 import org.apache.hadoop.fs.FSDataInputStream; 8 import org.apache.hadoop.fs.FSDataOutputStream; 9 import org.apache.hadoop.fs.FileSystem; 10 import org.apache.hadoop.fs.Path; 11 12 public class H_AppendorBefore { 13 public static void DelFile(FileSystem fs, Path p_remotepath) { 14 try { 15 if (fs.delete(p_remotepath, true)) { 16 ; 17 } 18 } catch (Exception e) { 19 e.printStackTrace(); 20 } 21 22 } 23 24 public static void appendToFileBefore(FileSystem fs, String localFilePath, 25 String remoteFilePath) { 26 Path remotePath = new Path(remoteFilePath); 27 28 try { 29 FileInputStream in_local = new FileInputStream(localFilePath); 30 FSDataInputStream in_remote = fs.open(remotePath); 31 DelFile(fs, remotePath); 32 FSDataOutputStream out = fs.create(remotePath); 33 out.close(); 34 out = fs.append(remotePath); 35 byte[] data = new byte[1024]; 36 int read = -1; 37 while ((read = in_local.read(data)) > 0) { 38 out.write(data, 0, read); 39 } 40 while ((read = in_remote.read(data)) > 0) { 41 out.write(data, 0, read); 42 } 43 out.close(); 44 System.out.println("write_before success"); 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } 48 } 49 50 public static void appendToFile(FileSystem fs, String localFilePath, 51 String remoteFilePath) { 52 Path remotePath = new Path(remoteFilePath); 53 try { 54 FileInputStream in = new FileInputStream(localFilePath); 55 FSDataOutputStream out = fs.append(remotePath); 56 byte[] data = new byte[1024]; 57 int read = -1; 58 while ((read = in.read(data)) > 0) { 59 out.write(data, 0, read); 60 } 61 out.close(); 62 System.out.println("write_append success"); 63 } catch (IOException e) { 64 e.printStackTrace(); 65 } 66 } 67 68 public static void main(String[] args) { 69 try { 70 Var_init var = new Var_init(); 71 Scanner sc = new Scanner(System.in); 72 System.out.println("input wa to write_append, wb to write_before"); 73 String str = sc.next(); 74 if (str.equals("wa")) { 75 appendToFile(var.fs, var.s_localFilePath, var.s_remoteFilePath); 76 } else if (str.equals("wb")) { 77 appendToFileBefore(var.fs, var.s_localFilePath, 78 var.s_remoteFilePath); 79 } 80 } catch (Exception e) { 81 e.printStackTrace(); 82 } 83 } 84 85 }
Var_init类参考 https://www.cnblogs.com/MiraculousB/p/13848744.html