public class FileUtil {
private Logger logger = (Logger) LoggerFactory.getLogger(FileUtil.class);
public void readFileFromInternet(String fileUrl, String fileName) throws IOException {
InputStream inputStream = new URL(fileUrl).openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String lineStr = null;
while((lineStr = br.readLine()) != null){
logger.info(lineStr);
}
}
}
用guava读取文件:
public void readFileFromInternet(String fileUrl, String fileName) throws IOException {
URL url = new URL(fileUrl);
List<String> stringList = Resources.readLines(url, Charsets.UTF_8);
String lineStr = null;
for (String str:stringList) {
logger.info(str);
}
}
测试代码:
public class FileUtilTest {
private Logger logger = (Logger) LoggerFactory.getLogger(FileUtilTest.class);
@Test
public void testReadFileFromInternet() throws IOException {
FileUtil fileUtil = new FileUtil();
fileUtil.readFileFromInternet("http://123.com/sites/task3.properties","test.txt");
}
}