java如何实现txt下载 就是从远程FTP服务器上直接把TXT文本下载下来!

java如何实现txt下载 就是从远程FTP服务器上直接把TXT文本下载下来!,第1张

strUrl为文件地址,fileName为文件在本地的保存路首祥径,试试吧~

public static void writeFile(String strUrl, String fileName) {

URL url = null

try {

url = new URL(strUrl)

} catch (MalformedURLException e2) {

e2.printStackTrace()

}

InputStream is = null

try {

is = url.openStream()

} catch (IOException e1) {

e1.printStackTrace()

}

OutputStream os = null

try {

os = new FileOutputStream( fileName)

int bytesRead = 0

byte[] buffer = new byte[8192]

while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {

os.write(buffer, 0, bytesRead)

}

System.out.println("者岁搏下载雀谈成功:"+strUrl)

} catch (FileNotFoundException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

}

}

import java.io.File

import java.io.FileNotFoundException

import java.io.IOException

import java.io.RandomAccessFile

public class Io{

public static void main(String [] s){

File filename = new File("F:\\suncity.txt"仿链)

String filein="你好!备磨孙"

RandomAccessFile mm = null

try {

mm = new RandomAccessFile(filename,"rw")

mm.writeBytes(filein)

} catch (IOException e1) {

// TODO 自动生成游空 catch 块

e1.printStackTrace()

} finally{

if(mm!=null){

try {

mm.close()

} catch (IOException e2) {

// TODO 自动生成 catch 块

e2.printStackTrace()

}

}

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12276406.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存