
把程序打成jar包放到Linux上
转到目录下执行命令 hadoop jar mapreducerjar /home/clq/export/java/countjar hdfs://ubuntu:9000/out06/count/
上面一个是本地文件,一个是上传hdfs位置
成功后出现:打印出来,你所要打印的字符。
package comclqhdfs;
import javaioBufferedInputStream;
import javaioFileInputStream;
import javaioIOException;
import javaioInputStream;
import javanetURI;
import orgapachehadoopconfConfiguration;
import orgapachehadoopfsFSDataOutputStream;
import orgapachehadoopfsFileSystem;
import orgapachehadoopfsPath;
import orgapachehadoopioIOUtils;
import orgapachehadooputilProgressable;
public class FileCopyWithProgress {
//
//把本地的一个文件拷贝到hdfs上
//
public static void main(String[] args) throws IOException {
String localSrc = args[0];
String dst = args[1];
InputStream in = new BufferedInputStream(new FileInputStream(localSrc));
Configuration conf = new Configuration();
FileSystem fs = FileSystemget(URIcreate(dst), conf);
FSDataOutputStream out = fscreate(new Path(dst), new Progressable() {
@Override
public void progress() {
Systemoutprint("");
}
});
IOUtilscopyBytes(in, out, conf, true);
}
}
可能出现异常:
Exception in thread "main" orgapachehadoopipcRemoteException: javaioIOException: Cannot create /out06; already exists as a directory
at orgapachehadoophdfsservernamenodeFSNamesystemstartFileInternal(FSNamesystemjava:1569)
at orgapachehadoophdfsservernamenodeFSNamesystemstartFile(FSNamesystemjava:1527)
at orgapachehadoophdfsservernamenodeNameNodecreate(NameNodejava:710)
at orgapachehadoophdfsservernamenodeNameNodecreate(NameNodejava:689)
at sunreflectGeneratedMethodAccessor7invoke(Unknown Source)
at sunreflectDelegatingMethodAccessorImplinvoke(DelegatingMethodAccessorImpljava:43)
at javalangreflectMethodinvoke(Methodjava:606)
at orgapachehadoopipcRPC$Servercall(RPCjava:587)
at orgapachehadoopipcServer$Handler$1run(Serverjava:1432)
at orgapachehadoopipcServer$Handler$1run(Serverjava:1428)
at javasecurityAccessControllerdoPrivileged(Native Method)
at javaxsecurityauthSubjectdoAs(Subjectjava:415)
说明你这个路径在hdfs上已经存在,换一个即可。
一、复制文件代码
printimport javaio;
import javautil;
class Copy
{
static void copy(String from,String to) throws IOException
{
BufferedReader in=new BufferedReader(new FileReader(from));
BufferedWriter out=new BufferedWriter(new FileWriter(new File(to)));
String line=null;
while((line=inreadLine())!=null)
{
outwrite(line+"\n");
}
inclose();
outclose();
}
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(Systemin);
Systemoutprint("请输入文件路径:");
String from=scnext();
Systemoutprint("请输入拷贝路径:");
String to=scnext();
copy(from,to);
}
}
import javaio;
import javautil;
class Copy
{
static void copy(String from,String to) throws IOException
{
BufferedReader in=new BufferedReader(new FileReader(from));
BufferedWriter out=new BufferedWriter(new FileWriter(new File(to)));
String line=null;
while((line=inreadLine())!=null)
{
outwrite(line+"\n");
}
inclose();
outclose();
}
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(Systemin);
Systemoutprint("请输入文件路径:");
String from=scnext();
Systemoutprint("请输入拷贝路径:");
String to=scnext();
copy(from,to);
}
}
二、复制文件夹代码 >
import javaio;
public class FileCopyDemo{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new FileReader("D:\\1txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\copytxt"));
String line = "";
while((line=brreadLine())!=null){
bwwrite(line);
bwnewLine();
bwflush();
}
bwclose();
brclose();
}
}
你好:
请看代码:
/把一个文件夹里的所有文件包括文件夹 一并原样拷贝到另一个目录中;
@author shuishui
/
import javaioFile;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStream;
import javaioOutputStream;
public class CopyDir001 {
public static File dirFrom;
public static File dirTo;
// 目标路径创建文件夹
public void listFileInDir(File file) {
File[] files = filelistFiles();
for (File f : files) {
String tempfrom = fgetAbsolutePath();
String tempto = tempfromreplace(dirFromgetAbsolutePath(),
dirTogetAbsolutePath()); // 后面的路径 替换前面的路径名
if (fisDirectory()) {
File tempFile = new File(tempto);
tempFilemkdirs();
listFileInDir(f);
} else {
Systemoutprintln("源文件:" + fgetAbsolutePath());
//
int endindex = temptolastIndexOf("\\");// 找到"/"所在的位置
String mkdirPath = temptosubstring(0, endindex);
File tempFile = new File(mkdirPath);
tempFilemkdirs();// 创建立文件夹
Systemoutprintln("目标点:" + tempto);
copy(tempfrom, tempto);
}
}
}
/
封装好的文件拷贝方法
/
public void copy(String from, String to) {
try {
InputStream in = new FileInputStream(from);
OutputStream out = new FileOutputStream(to);
byte[] buff = new byte[1024];
int len = 0;
while ((len = inread(buff)) != -1) {
outwrite(buff, 0, len);
}
inclose();
outclose();
} catch (FileNotFoundException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
}
public static void main(String[] args) {
File fromfile = new File("e:\\shui\\test");// 源文件夹
File tofile = new File("e:\\Jying\\shui");// 目标
CopyDir001 copy = new CopyDir001();
// 设置来源去向
copydirFrom = fromfile;
copydirTo = tofile;
copylistFileInDir(fromfile);
}
}
最简单的io流问题,不用什么高手,
我给你写个方法,参数是2个字符串,第一个写原文件的全路径,第二个写目标文件的全路进。 你试试吧
public void copy(String fromFilePath, String toFilePath) {
try {
FileInputStream fis = new FileInputStream(fromFilePath);
FileOutputStream fos = new FileOutputStream(toFilePath);
byte[] b = new byte[100];
try {
while (fisread(b) != (-1)) {
foswrite(b);
}
if (fis != null) {
fisclose();
fis = null;
}
if (fos != null) {
fosflush();
fosclose();
fos = null;
}
} catch (IOException e) {
Systemoutprintln("io异常");
}
} catch (FileNotFoundException e) {
Systemoutprintln("源文件不存在");
}
public static void main(String[] args) {
//自己把路径补齐,别忘了!!!!!!!!!!!!!!!!
String fromFilePath=" "; // 源文件的全路径。 比方"d://myphoto//nihaomp3"
String toFilePath=" "; //目标文件的全路劲。 如果不存在会自动建立,如存在则在文件尾继续添加
new CopyTest()copy(fromFilePath, toFilePath);
}
}
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioIOException;
public class FileCopy {
public static void main(String[] args) throws IOException {
String src = null;
String desc = null;
try{
src = args[0];
desc = args[1];
}catch(ArrayIndexOutOfBoundsException noFileExp){
Systemoutprintln("请传递要拷贝的文件名字");
return;
}
FileInputStream srcFile = new FileInputStream(src);
FileOutputStream descFile = new FileOutputStream(desc);
byte[] ary = null;
int byteRead = -1;
do{
ary = new byte[1024];
byteRead = srcFileread(ary);
descFilewrite(ary);
descFileflush();
}while(byteRead != -1);
srcFileclose();
descFileclose();
}
}
----------------------testing
C:\Program Files\IBM\RAD 7\jdk\bin>java FileCopyclass c:\aajpg c:\ccjpg
C:\Program Files\IBM\RAD 7\jdk\bin>
package chapter06sy6_3;
import javaio;
class test3_demo
{
public static void main(String[]args)
{
//定义BufferedReader处理流的引用
BufferedReader br=null;
//定义BufferedWriter处理流的引用
BufferedWriter bw=null;
//定义BufferedWriter处理流的引用
BufferedWriter out=null;
try
{
//将指定的FileReader节点流封装成处理流
br=new BufferedReader(new FileReader("sy6_3java"));
//将指定的FileWriter节点流封装成处理流
bw=new BufferedWriter(new FileWriter("Testtxt"));
//将BufferedWriter处理流再封装成处理流
out=new PrintWriter(bw);
}
catch(IOException e){
eprintStackTrace();
}
//将读取的一行内容写入指定的文件
outprintln(s);
//将读取的一行内容打印在控制台
Systemoutpritln(s);
}
}
import javaioBufferedReader;
import javaioBufferedWriter;
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStreamReader;
import javaioOutputStreamWriter;
public class mytxt {
public static void main(String[] args) throws IOException {
String infile = args[1]toString(); // 输入文件
String fileout = "resulttxt"; // 输出路径
try {
// 输出结果
File f = new File(fileout);
FileOutputStream fout = new FileOutputStream(f);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
// 输入文件
BufferedReader br = null;
File file = new File(infile);
if (fileexists()) {
FileInputStream fin = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fin));
}
String str = brreadLine();
int n = 0;
while (str != null && !strisEmpty()) {
n++;
str = brreadLine();
bwwrite(str + "\n");
}
bwflush();
bwclose();
brclose();
} catch (Exception e) {
eprintStackTrace();
}
}
}
[还望自行修改,感觉Swing 很鸡肋 。]
以上就是关于如何用java程序把本地文件拷贝到hdfs上并显示进度全部的内容,包括:如何用java程序把本地文件拷贝到hdfs上并显示进度、java 编写程序,拷贝一个带内容的文件夹。 例如:将c:\program files\java 文件夹拷贝到D盘的根目录下、2. 用Java编写一个程序,使用字符流拷贝一个文本文件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)