要求:读取hdfs的文件/ggg1.txt到控制台
代码如下:会由乱码!!!
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;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
@Test
public void test1() throws IOException, URISyntaxException, InterruptedException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), conf, "root");
FSDataInputStream fis = fs.open(new Path("/ggg1.txt"));
FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\管健雄\\Desktop\\files\\ggj.txt"), true);
String line;
while ((line = fis.readLine()) != null) {
System.out.println(line);
}
fos.close();
fis.close();
fs.close();
}
更改代码如下
@Test
public void test2() throws URISyntaxException, IOException, InterruptedException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), conf, "root");
FSDataInputStream fis = fs.open(new Path("/ggg1.txt"));
BufferedReader bf = new BufferedReader(new InputStreamReader(fis));
String line;
while ((line = bf.readLine()) != null) {
System.out.println(line);
}
}
未出现乱码