Please help, very urgent, Reading from file containing url list and converting them into a List of user defined objects
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, July 25, 2006 02:53 AM

I have a text file called "FileUrlsList.txt". In that file, there will be a list of urls. Eg: file://java.sun.com/images/Img1.gif file://java.sun.com/images/Img1.gif file://java.sun.com/images/Img1.gif file://java.sun.com/images/Img1.gif . . or C:/Images/Img1.gif C:/Images/Img2.gif C:/Images/Img3.gif . . There will be corresponding images for these urls. What i want is : 1) a function which reads this file and converts each line into an object like : FileEntry    More>>

I have a text file called "FileUrlsList.txt".

In that file, there will be a list of urls.


Eg:

file://java.sun.com/images/Img1.gif

file://java.sun.com/images/Img1.gif

file://java.sun.com/images/Img1.gif

file://java.sun.com/images/Img1.gif

.

.


or


C:/Images/Img1.gif

C:/Images/Img2.gif

C:/Images/Img3.gif

.

.




There will be corresponding images for these

urls. What i want is :


1) a function which reads this file and converts each

line into an object like :


FileEntry

URL url;

String name;



FileEntry(String name, URL url);



and add these objects to a java.util.List and return

the list. The name will be the name of each files in

the url ( without extension) and URL will be the URL

object reprsenting each line in the file.


The main aim is, later if i want, i should be able to

read the contents of each file mentioned in

the "FileUrlsList.txt"

with the help of these "FileEntry" object.

   <<Less

Re: Please help, very urgent, Reading from file containing url list and converting them into a List of user defined objects

Posted By:   Robert_Lybarger  
Posted On:   Tuesday, July 25, 2006 10:13 PM

You are asking someone to provide a bit more code than most people will probably volunteer time to do. This non-working code fragment might get you pointed in the right direction. You will need to add try/catch blocks around a few things for a compiler to accept it completely, for example:


List list = new ArrayList();

File urlListFile = new File("FileUrlsList.txt");
BufferedReader BR = new BufferedReader(
new FileReader(urlListFile));
String line = null;
while ((line = BR.readLine()) != null) {

// add code to skip blank lines, comment lines, etc.

int lastSlashPos = line.lastIndexOf("/");
// add code to handle a -1 return value
String fileName = line.substring(lastSlashPos);

int lastDotPos = fileName.lastIndexOf(".");
// same comment about -1 return value
String fileNamePrefix = fileName.substring(0,lastDotPos);

list.add(new FileEntry(fileNamePrefix, new URL(line)));
}

BR.close();
return list;
About | Sitemap | Contact