字符串保存到本地
public class Save {
public static void main(String[] args) throws IOException {
String name="王八蛋";
byte[] by=name.getBytes();
FileOutputStream out=new FileOutputStream("F:/a.txt");
int len;
for(len=0;len<by.length;len++) {
out.write(by[len]);
}
out.close();
}
}
网络获取图片保存到本地
public class Netsave {
public static void main(String[] args) throws IOException {
URL url=new URL("https://www.baidu.com/favicon.ico");
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(5000);
InputStream inputstream=con.getInputStream();
OutputStream out=new FileOutputStream("F:/a.png");
byte[] buffer=new byte[1024];
int len=0;
while((len=inputstream.read(buffer))!=-1) {
out.write(buffer, 0, len);
}
inputstream.close();
out.close();
}
}