Java Help reading in the contents of a text file on my web-space

Hi

None of the examples I've found are correct.

I'd like to import the text from a text file on my web-space into a String Object in my code.

Thanks
LVL 1
beavoidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPColinSenior Java ArchitectCommented:
To clarify, you're trying to fetch the contents of a URL and store them in a String, right? You should be able to open a URL connection, get its InputStream, wrap that in an InputStreamReader, wrap that in a BufferedReader, call BufferedReader.readLine() repeatedly, and add each line to a StringBuilder. What examples have you tried that aren't working? And what parts are breaking?
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
1
beavoidAuthor Commented:
Hi Thanks

This is the code that worked for me:

public static void printURLfile() {
		
		try {
			URL oracle = new URL("http://... ... ... DKSalaries.csv");
	        BufferedReader in = new BufferedReader(
	        new InputStreamReader(oracle.openStream()));

	        String inputLine;
	        while ((inputLine = in.readLine()) != null)
	            System.out.println(inputLine);
	        in.close();
		} catch (Exception e) {

			System.out.println("printURLfile ex : "+e);
			e.printStackTrace();
		}
	}

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

beavoidAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for beavoid's comment #a41272604
Assisted answer: 500 points for CPColin's comment #a41270433

for the following reason:

Thanks
simple and clean
0
CEHJCommented:
Why would you want to read a CSV file into a String?
0
beavoidAuthor Commented:
True

I have it as an array of strings
0
CEHJCommented:
OK, well let's hope you're not reading it as a string then calling split[] or using a StringTokenizer or something
0
beavoidAuthor Commented:
no - it goes into a array of an appropriate struct.
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.