導航:首頁 > 編程語言 > javafile創建

javafile創建

發布時間:2025-08-09 16:38:25

java 怎麼使用遠程 url 創建 file

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;

/**
*@authorlmq
*
*/
publicclassRemoteFile{

publicstaticvoidmain(String[]args)throwsException{
FileremoteFile=newFile("//192.168.7.146/test/1.txt");//192.168.7.146是對方機器IP,test是對方那個共享文件夾名字,如果沒有共享是訪問不到的
//遠程文件其實主要是地址,地址弄對了就和本地文件沒什麼區別,windows裡面//或者\\開頭就表示這個文件是網路路徑了其實這個地址就像我們再windows裡面,點擊開始
//然後點擊運行,然後輸入\192.168.7.146/test/1.txt訪問遠程文件一樣的

BufferedReaderbr=newBufferedReader(newFileReader(remoteFile));
Stringstr;
while((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
}
}

如果是非共享文件 你只能通過url讀取流來生成了

publicvoiddownUrlTxt(StringfileName,StringfileUrl,StringdownPath){
FilesavePath=newFile(downPath);
if(!savePath.exists()){
savePath.mkdir();
}
String[]urlname=fileUrl.split("/");
intlen=urlname.length-1;
Stringuname=urlname[len];//獲取文件名
try{
Filefile=newFile(savePath+"/"+uname);//創建新文件
if(file!=null&&!file.exists()){
file.createNewFile();
}
OutputStreamoputstream=newFileOutputStream(file);
URLurl=newURL(fileUrl);
HttpURLConnectionuc=(HttpURLConnection)url.openConnection();
uc.setDoInput(true);//設置是否要從URL連接讀取數據,默認為true
uc.connect();
InputStreamiputstream=uc.getInputStream();
System.out.println("filesizeis:"+uc.getContentLength());//列印文件長度
byte[]buffer=newbyte[4*1024];
intbyteRead=-1;
while((byteRead=(iputstream.read(buffer)))!=-1){
oputstream.write(buffer,0,byteRead);
}
oputstream.flush();
iputstream.close();
oputstream.close();
}catch(Exceptione){
System.out.println("讀取失敗!");
e.printStackTrace();
}
System.out.println("生成文件路徑:"+downPath+fileName);
}
閱讀全文

與javafile創建相關的資料

熱點內容
網路中常用的傳輸介質 瀏覽:518
文件如何使用 瀏覽:322
同步推密碼找回 瀏覽:865
樂高怎麼才能用電腦編程序 瀏覽:65
本機qq文件為什麼找不到 瀏覽:264
安卓qq空間免升級 瀏覽:490
linux如何刪除模塊驅動程序 瀏覽:193
at89c51c程序 瀏覽:329
怎麼創建word大綱文件 瀏覽:622
裊裊朗誦文件生成器 瀏覽:626
1054件文件是多少gb 瀏覽:371
高州禁養區內能養豬多少頭的文件 瀏覽:927
win8ico文件 瀏覽:949
仁和數控怎麼編程 瀏覽:381
項目文件夾圖片 瀏覽:87
怎麼在東芝電視安裝app 瀏覽:954
plc顯示數字怎麼編程 瀏覽:439
如何辨別假網站 瀏覽:711
寬頻用別人的賬號密碼 瀏覽:556
新app如何佔有市場 瀏覽:42

友情鏈接