Skip to content

Instantly share code, notes, and snippets.

@strongant
Created May 20, 2016 07:18
Show Gist options
  • Save strongant/d3239d6b664fab80fff72d9518a9cf13 to your computer and use it in GitHub Desktop.
Save strongant/d3239d6b664fab80fff72d9518a9cf13 to your computer and use it in GitHub Desktop.
package com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Created by devbwh on 16-5-20.
*/
public class FileTest {
public static void main(String[] args) {
try {
String sourcePath="https://gist.githubusercontent.com/CootCraig/6186298/raw/26fb35a849fac60cb3653e9b8b9877af5ce39f66/logger.js";
readHtmlFile(sourcePath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取文件
* @param sourcePath 文件所在的网络路径
*/
public static void readHtmlFile(String sourcePath){
String line;
int lineNum=0;
BufferedReader reader=null;
try{
URL url = new URL(sourcePath);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((line = reader.readLine()) != null){
lineNum++;
System.out.println(line);
}
}
catch (Exception ie){
ie.printStackTrace();
}finally{
try{
if(reader != null)
reader.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment