Use Java BufferedReader to read files from the network to local, to be stored in a database, or to be saved to local Java code
1. Download the contents of the network file into StringBuffer
/** 从网络地址url下载文件读成字符串 * @param downloadUrl 文件的网络地址 * @return */public static StringBuffer downloadFromUrl(String downloadUrl) { BufferedReader reader = null; StringBuffer stringBuffer = new StringBuffer(); String line; try { URL url = new URL(downloadUrl); reader = new BufferedReader(new InputStreamReader(url.openStream())); while ((line = reader.readLine()) != null) { stringBuffer.append(line); } return stringBuffer; } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (Exception e) { e.printStackTrace(); } } return null;}
2. Save a string to a file of the specified path
public static void saveAsFile(String content,String fileName) throws FileNotFoundException { File file=new File(fileName); PrintStream ps =new PrintStream(new FileOutputStream(file)); ps.append(content); ps.flush();}
Java BufferedReader Download Network files