Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: returning variable and saving to file

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default returning variable and saving to file

    Hi my program downloads image links from www . I want the the image links wich are displaed to be also saved to text file on "c:\file.txt". I was looking for some examples but i couldnt do it, can someone help me and do this for me . At the end of code there's "return listaLinkow;" i want this results to be saved to text file. Thank you for help



    package imagelinks;
     
    import javax.jws.WebService;
     
     
    import java.net.*;
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import javax.swing.filechooser.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
     
     
    @WebService(targetNamespace = "http://imglinks/")
    public class ImageLinks implements IImageLinks {
     
     
        // sprawdz format URL
        private static URL sprawdzUrl(String url) {
          // tylko URLs. zaczynające się od http
          if (!url.toLowerCase().startsWith("http://"))
            return null;
          URL sprawdzonyUrl = null;
          try {
            sprawdzonyUrl = new URL(url);
          } catch (Exception e) {
            return null;
          }
          return sprawdzonyUrl;
        }
     
        // Pobierz stronę o danym URL.
        private static String downloadPage(URL pageUrl) {
          try {
            // Open connection to URL for reading.
            BufferedReader reader = new BufferedReader(new InputStreamReader(pageUrl.openStream()));
            // Read page into buffer.
            String line;
            StringBuffer pageBuffer = new StringBuffer();
            while ((line = reader.readLine()) != null) {
              pageBuffer.append(line);
            }
            return pageBuffer.toString();
          }
          catch (Exception e) {
          }
          return null;
        }
     
        private static ArrayList<String> pobierzImageLinks(String pageContents) {
     
            ArrayList links = new ArrayList();
     
       // 2    Pattern p = Pattern.compile("<\\s*img\\s[^>]*src\\s*=\\s*\"([^\\\"]*)(\")[^>]*>"       , Pattern.CASE_INSENSITIVE);
         Pattern p = Pattern.compile("<img [^>]*\\bsrc=\"([^\"]*)\"", Pattern.CASE_INSENSITIVE);
     
            Matcher m = p.matcher(pageContents);
     
            while (m.find()) {
                String link = m.group(1).trim();
                if (link.length() < 1) {
                    continue;
                }
                if (link.charAt(0) != 'h') {
                    continue;
                }
                links.add(link);
            }
     
            return links;
        }
     
        public ArrayList<String> getImageLinks(String strona) {
     
            System.out.println("Pobranie linków z strony: " + strona);
     
            URL url = sprawdzUrl(strona);
            String zawartosc = downloadPage(url);
            ArrayList listaLinkow = pobierzImageLinks(zawartosc);
     
     
            return listaLinkow;
     
        }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: returning variable and saving to file

    What is the problem in your code?
    Does it throw any exception?
    No one here will write code for you. But we will teach you to go through the problems you have created in your code and will help you to solve them on your own by providing you hints.

  3. #3
    Member
    Join Date
    Nov 2011
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: returning variable and saving to file

    Hi,
    I do not think there should be any problem to save the links in a file try google you can find your answer or simple append everytime the link in a file.

    Core java
    Last edited by mr.miku; January 11th, 2012 at 07:19 PM.

Similar Threads

  1. Saving to a text file
    By PsYNus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2011, 11:57 AM
  2. Saving a file using a dialog box
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2010, 05:21 PM
  3. saving xml file
    By LOL in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 18th, 2010, 05:45 AM
  4. [SOLVED] Saving A File?
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 1st, 2009, 11:56 AM
  5. How to save statistics of a game in a binary file instead of txt file?
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 19th, 2009, 05:05 AM