讀取本地 文件 和 URL網絡 文件 的 兩個例子

1  讀取 本地文件

注意 InputStream  和 StringBuffer的使用,以及 一行一行讀取的語句!

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Read{

    
private static String openFile(String filePath) {

        File f 
= new File(filePath);
        String ee 
= new String();

        System.out.println(
"Opening file: " + filePath);
        
try 
        
{
            InputStream is 
= new FileInputStream(f);
            BufferedReader reader 
= new BufferedReader(
                    
new InputStreamReader(is));
            StringBuffer buffer 
= new StringBuffer();
            String line; 
// 用來保存每行讀取的內容
            line = reader.readLine(); // 讀取第一行
            while (line != null// 如果 line 爲空說明讀完了
                buffer.append(line); // 將讀到的內容添加到 buffer 中
                buffer.append(" "); // 添加換行符
                line = reader.readLine(); // 讀取下一行
            }

//            System.out.print(buffer.toString());
            
            
            ee 
= buffer.toString();
            
            
        }
 
        
        
catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 
        
catch (IOException e) {
            e.printStackTrace();
        }
 
        
        
return  ee;
        

    }

    
    
public static void main(String[] args){
        System.out.print(Read.openFile(
"l:/fence.log"));
    }

}

 2  讀取URL制定的文件

      是在上面的代碼基礎上,改制而成。

     注意這一段代碼,由連接 給定 的 URL 而取出輸入流!,然後進行常規的讀取操作!

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

URLConnection urlconn = url.openConnection(); // 試圖連接並取得返回狀態碼
   urlconn.connect();
   HttpURLConnection httpconn =(HttpURLConnection)urlconn;
   HttpResult = httpconn.getResponseCode();
   if(HttpResult != HttpURLConnection.HTTP_OK) // 不等於HTTP_OK說明連接不成功
    System.out.print("無法連接到");
   else
   {
    int filesize = urlconn.getContentLength(); // 取數據長度
    InputStreamReader isReader = new InputStreamReader(urlconn.getInputStream());

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class ReadURL{

    
private static String openFile(String filePath) {
    
        
int HttpResult; // 服務器返回的狀態

        String ee 
= new String();

        
try 
        
{
            URL url 
=new URL(filePath); // 創建URL
            URLConnection urlconn = url.openConnection(); // 試圖連接並取得返回狀態碼
            urlconn.connect();
            HttpURLConnection httpconn 
=(HttpURLConnection)urlconn;
            HttpResult 
= httpconn.getResponseCode();
            
if(HttpResult != HttpURLConnection.HTTP_OK) // 不等於HTTP_OK說明連接不成功
                System.out.print("無法連接到");
            
else
            
{
                
int filesize = urlconn.getContentLength(); // 取數據長度
                InputStreamReader isReader = new InputStreamReader(urlconn.getInputStream());

                BufferedReader reader 
= new BufferedReader(isReader);
                StringBuffer buffer 
= new StringBuffer();
                String line; 
// 用來保存每行讀取的內容
                line = reader.readLine(); // 讀取第一行
                while (line != null// 如果 line 爲空說明讀完了
                    buffer.append(line); // 將讀到的內容添加到 buffer 中
                    buffer.append(" "); // 添加換行符
                    line = reader.readLine(); // 讀取下一行
                }

//                System.out.print(buffer.toString());
                
                
                ee 
= buffer.toString();
            }

            

            
            
        }
 
        
        
catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 
        
catch (IOException e) {
            e.printStackTrace();
        }
 

        
        
return  ee;
        

    }

    
    
public static void main(String[] args){
        System.out.print(ReadURL.openFile(
"http://166.111.180.102:8080/axis/services/Read?wsdl"));
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章