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


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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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?

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
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)
		} catch (Exception e) {

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

Open in new window

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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:

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

I have it as an array of strings
OK, well let's hope you're not reading it as a string then calling split[] or using a StringTokenizer or something
beavoidAuthor Commented:
no - it goes into a array of an appropriate struct.
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

From novice to tech pro — start learning today.