簡體   English   中英

使用用戶輸入的文件名創建Java編程來編寫網頁源代碼

[英]Creating a java programming to webpage source code with user input file name

我正在嘗試處理將源代碼捕獲到文件中的程序。 我嘗試以其他方式使其正常工作,但似乎無法正常工作。 例如,我想捕獲網頁源代碼,並允許用戶將程序保存為.txt格式。 有人可以幫我嗎? `

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.io.File;
import java.io.FileWriter;


public class ReadFromWeb {
    public static void readFromWeb(String webURL) throws IOException {
        URL url = new URL(webURL); // create a new url 
        InputStream is =  url.openStream(); //input 

        //read url 
        try( BufferedReader br = new BufferedReader(new InputStreamReader(is))) { 
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
            throw new MalformedURLException("URL is malformed!!");
        }
        catch (IOException e) {
            e.printStackTrace();
            throw new IOException();
        }

    }




    public static void main(String[] args) throws IOException {
        Scanner login = new Scanner(System.in);
        Scanner cn= new Scanner(System.in); //create case name scaner
        Scanner sc = new Scanner(System.in); // create the scanner for capture webpage

        String Username;
        String Password;

        Username = "steven";
        Password = "1234";

        System.out.println("enter username : ");
        String username = login.next();

        System.out.println("enter password : ");
        String password= login.next();

        if (username.equals(Username)&& (password.equals(Password))){
           System.out.println("logged in");



        //create file name and save file

        System.out.println("enter case number :"  );
        String input = cn.nextLine().trim();
        File file = new File(input);

        file.createNewFile();

        //write into file
        FileWriter writer = new FileWriter(file);



        System.out.println("enter URL : ");
        String print;
        String url = sc.nextLine();  // read the URL
        readFromWeb(url); //show the url source data
       // writer.write(print); //write into file
       // writer.close(); //write close
        }
        else if (username.equals(Username)){ //invalid password

            System.out.println ("invalid password");

        }
        else if (password.equals(Password)){ //invalid username
            System.out.println("Invalid username");

    }
        else { //invalid bth username and password
            System.out.println("invalid username & password");
            System.exit(0);
        }



}
}

`

因此,基本上該程序要求用戶登錄,然后文件名將與用戶輸入的情況相同。 之后,用戶粘貼網址,系統將捕獲該網址並將其保存到文件中。 但是可行的方法是我無法將文件保存到用戶輸入的文件名中。

您甚至都沒有嘗試向輸出文件中寫入內容。

我看到三種可能的解決方案:

1.讓您的方法寫輸出

為此,更改readFromWeb的名稱以download並聲明第二個參數,該參數采用File 然后,讓while循環將line寫入文件,而不是stdout

優點:

  • 內存占用小(最大行大小)

缺點:

  • 由於您的方法同時兼顧了兩者 ,因此無法很好地分離關注點

2.讓您的方法返回內容並將其寫入文件

無需將行寫到stdout ,而是將StringBuilder附加到一個大的胖String ,然后從您的方法中將其返回(不要忘記手動添加換行符)。

優點:

  • 關注點分離(從Web讀取/輸出到文件)

缺點:

  • 一個大緩沖區(不適用於大文件)

3. 返回 使用線路流

而不是通過while循環處理方法中的行,而是使用BufferedReaderlines()方法獲得的線流。 然后將forEach()PrintWriteprintln()方法一起使用(您需要在其中放置FileWriter這是必需的,因為append()不會給您換行符)。

優點:

  • 使用使您的意圖明確的功能概念
  • 同樣,不需要大的緩沖

缺點:

  • 讀/寫也不用這種方法分開
  • 仍有更好的方法可以做到這一點,但這應該會讓您感到高興。

更新 :由於最初的想法需要對BufferedReader實例進行更復雜的管理,因此我更新了答案,以直接使用方法中的行流。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM