在txt文件中写点需要上传的数据,conf是source包,lib是jar包
package com.wangbo.day0830; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; /** * hdfs操作类 * @author Administrator * */ public class HdfsTest { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //创建hdfs的配置 Configuration conf = new Configuration(); //加载配置文件 conf.addResource("conf/core-site.xml"); conf.addResource("conf/hdfs-site.xml"); //获取HDFS的句柄 FileSystem fs = FileSystem.get(conf); // //向HDFS写入数据 // writeHDFS(fs); //向HDFS读数据 readHDFS(fs); } private static void readHDFS(FileSystem fs) throws IllegalArgumentException, IOException { //获取文件输入流 FSDataInputStream in = fs.open(new Path("syhz/hellohadoop.txt")); byte[] buff = new byte[1024]; BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("d:/hdfs.txt"))); // 写入文件 字节流 int length = in.read(buff); while(-1 != length) { // System.out.println(new String(buff, 0, length)); out.write(buff, 0, length); length = in.read(buff); } in.close(); out.close(); System.out.println("读取结束!"); } public static void writeHDFS(FileSystem fs) throws IOException { //在HDFS上新建一个虚拟目录 Path path = new Path("syhz/hellohadoop.txt"); //输出流 FSDataOutputStream out = fs.create(path); //读取数据写入数据 字节流 // BufferedInputStream bufferIn = new BufferedInputStream(new FileInputStream(new File("text.txt"))); // byte[] buff = new byte[1024]; // int length = bufferIn.read(buff); // // while(-1 != length) // { // //写入 // out.write(buff,0,length); // out.flush(); // length = bufferIn.read(buff); // } // out.close(); // bufferIn.close(); //读取数据写入数据 字符流 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("text.txt")),"gbk")); String line = null; while((line = reader.readLine())!=null){ out.write((line+System.getProperty("line.separator")).getBytes("utf-8")); out.flush(); } out.close(); reader.close(); } }就是酱紫