導航:首頁 > 編程語言 > 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創建相關的資料

熱點內容
c語言編程三個月能學成什麼樣 瀏覽:796
文件gdpt格式 瀏覽:803
系統升級下載文件在哪裡 瀏覽:777
xml文件如何用不同顏色標注 瀏覽:964
win10漏洞文件 瀏覽:624
學校教學樓網路布線圖 瀏覽:589
編程的image是什麼意思 瀏覽:665
在哪裡看國家經濟數據 瀏覽:429
jsp日期鑽換unix時間戳的函數 瀏覽:630
wpweixinqqcom 瀏覽:88
win81rt升級win10 瀏覽:97
聊天文件視頻文件怎麼打開 瀏覽:120
規劃類app 瀏覽:513
excel打開分為單獨文件 瀏覽:770
如何學編程網上教程 瀏覽:276
c語言如何寫文件 瀏覽:477
手機怎麼重命名文件 瀏覽:772
蘋果手機百度瀏覽器下載文件位置 瀏覽:54
數控銑床分八個點如何編程 瀏覽:711
dnf90版本斷腸絕傷 瀏覽:524

友情鏈接