Hello everyone..

I am trying to retrieve a URL of applications from a text file...

My text file contains 3 URLS for 3 applications say for example :

C:/ABC.java
C:/JVM.java
C:/Tomcat.java

I have tried implementing a code for the same but when I try to copy the URL into a string variable it functions abruptly...

When i try to print it, it gives unnecessary spaces and the output is not desirable..
However, if I print it character wise, I do get the required result but I am unable to retrieve one URL at a time...It prints all the 3..

Any help???

I will write down the code that I have tried to implement...

//read.java

import java.io.*;

class read {
    public static void main(String args[])
    throws IOException
    {
        int m=0;
        int i;
        String s1 = new String() ;
        char c[] = new char[50];    
        FileInputStream fin;

        try {
            fin = new FileInputStream("C:/Documents and Settings/Administrator/Desktop/Shweta_Project/Work_27_Jan_2008/URL.txt");
        } catch(FileNotFoundException e) {
            System.out.println("File not found");
            return;
        } catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("Usage : ShowFile File");
            return;
        }

        do {
            i = fin.read();
            if(i != -1) {

                char ch = (char) i;
                    c[m] = ch;
                    m++;
                if(c[m-1] == '*') {
                        c[m-1] = ' ';
                        s1 = new String(c);
                        System.out.print(s1);

                        s1 = null;
                            m=0;            
                }
            }
        } while(i != -1);
        fin.close();
    }
}


// MY FILE CONTENTS :
C:/ABC.java*
C:/JVM.java*
C:/Tomcat.java*

Please do help me...

Recommended Answers

All 2 Replies

Try using a BufferedReader with the readLine() method.

String line=null;
while ( (line=reader.readLine()) != null){
  // do stuff
}

I would also suggest using String.trim(), might resolve the erroneous spaces you said you were receiving.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.