Learning Java: Part 15: Reading Text from Files

Learning Java: Part 15: Reading Text from Files

BrandonioProductions

12 лет назад

136,786 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@SammyJoseph06
@SammyJoseph06 - 20.11.2015 05:44

Subscribed! <3

Ответить
@Matt-kh6te
@Matt-kh6te - 21.11.2015 05:00

As a tip in general it's a bad idea to add strings in a loop like this. But since here we know how big this file is and we know the file won't be expanded in the future it's no bid deal.

If this file was for instance inventory for a company or any file you'd expect to grow over time you should use the StringBuilder class for the string instead. The difference is StringBuilder will only use one string for the entire file whereas here we would write each line in the file as its own string.

the code differs like this:
StringBuilder s = new StringBuilder();

while (line!=null){
s.append(line+"\n");
line = reader.readLine();

}

Ответить
@LisandroGarcia86
@LisandroGarcia86 - 23.11.2015 20:17

thank you thank you thank you out of all the text read programs I wrote and tutorial for that sort of program yours is the only one that worked :D

Ответить
@syedsirajalijawwad5352
@syedsirajalijawwad5352 - 23.12.2015 13:30

there is no error coming during coding but program is not running, gives error at the time of running

Ответить
@syedsirajalijawwad5352
@syedsirajalijawwad5352 - 23.12.2015 13:30

please solve a problem

Ответить
@LovosMedia
@LovosMedia - 20.01.2016 00:18

had alot of problems with those filereaders. first time I understand everything. great tutorial! thanks!

Ответить
@learner6189
@learner6189 - 25.01.2016 15:30

sorry i don't understand when you using " text += line;" can anyone explain this to me?

Ответить
@General1Cal
@General1Cal - 28.01.2016 09:06

thanks for the vids I was so stuck on this.

Ответить
@Chobits099
@Chobits099 - 01.02.2016 09:33

How do i read a 2d array from a text file?

Ответить
@MoizTheRelaxer
@MoizTheRelaxer - 02.02.2016 04:51

Liked for the help then disliked for League

Ответить
@Arraying
@Arraying - 07.02.2016 17:46

Two questions:
1) How can I print 2 lines; so if I have hello in the first line and world in the second line how can I make it "Hello
World"

2) How can I make the path the path where the jar is located, because I need to be distributing the program so the path will change for all users.

Ответить
@peace957
@peace957 - 19.02.2016 01:50

very helpful and very well spoken. great job look forward to watching your videos in the future!

Ответить
@Phrosen1
@Phrosen1 - 21.02.2016 21:44

What happens with "throws Exception"?

Ответить
@an1792
@an1792 - 24.02.2016 23:52

great, can you please make a video where we can match if the two files have common data in them?
thanks for the great vedio

Ответить
@loubnayoussar6612
@loubnayoussar6612 - 04.03.2016 09:25

Very clear! Thanks a lot.

Ответить
@ibrahimroble3532
@ibrahimroble3532 - 13.04.2016 18:01

For the Lazy people:
FileReader file = new FileReader("C:/Users/ibrahim/Documents/NetBeansProjects/Test4_1/submit.txt");
BufferedReader reader = new BufferedReader(file);
String text = "";
String line = reader.readLine();
while (line != null) {
text += line;
line = reader.readLine();
}
System.out.println(text);

Ответить
@KJGamer-Official
@KJGamer-Official - 23.04.2016 00:34

hey i want to know how can i search for a key word, such as a barcode

for instance my textfile has the code "12345678" but their will be mutliple barcodes withing this file along with a product discription and price

Ответить
@FrinkyDinky
@FrinkyDinky - 18.05.2016 17:57

You actually explained this better than my uni lecturer.
You kept it concise, didn't stumble with your words and broke everything down simply without overloading it with technical bollocks.
Props to you sir.

Ответить
@ralphregiebrillo3295
@ralphregiebrillo3295 - 28.08.2016 03:41

How to save and open a file in your system? Thank you.

Ответить
@quigonmidd
@quigonmidd - 19.10.2016 05:03

Wow someone who finally can do a simple example after hours of searching. You sir, earned a subscriber.

Ответить
@harshavardhan8181
@harshavardhan8181 - 03.11.2016 08:03

how to read the file if we have more than one line..??
please help me.

Ответить
@YamadaDesigns
@YamadaDesigns - 21.11.2016 02:09

can you use filereader to input string text files like scanner?

Ответить
@SikarinPunsawat
@SikarinPunsawat - 26.11.2016 09:42

Do you know how to do
if I want to read string by string like function fsacnf in C
thank u (I'm beginner java)

Ответить
@mariajacinto3805
@mariajacinto3805 - 05.12.2016 02:03

thank you that was very helpfull!

Ответить
@KevinMartinez-xx7yc
@KevinMartinez-xx7yc - 07.02.2017 17:00

how come you don't have to close the file at the end?

Ответить
@AvaroLekuCWA
@AvaroLekuCWA - 08.04.2017 09:34

can you read multiple text files?

Ответить
@chailigen1998
@chailigen1998 - 21.04.2017 21:20

do you know how to ask user to input something and then write it into the file ???

Ответить
@Adam-cn5ib
@Adam-cn5ib - 21.05.2017 02:30

If you run into a problem searching for the file with eclipse you might wanna call it readme.txt.txt. Atleast that worked for me.

Ответить
@alkhashtee
@alkhashtee - 30.05.2017 09:20

Thank you very much Brandon and I really proud of you.
I sent you a friendship request on Facebook, hope you will accept it.
Regards

Ответить
@MuzzyV29
@MuzzyV29 - 04.07.2017 23:00

is Readline method read only a line, and is that why we had to loop readLine method, if so how does readLine method know it has to go to next line in the file for rest of the text ?

Ответить
@medatre4792
@medatre4792 - 04.08.2017 05:48

Thank so much!! Complete Java newbie over here. Glad I found this bc I had no idea how to get started :)

Ответить
@zenzeki751
@zenzeki751 - 04.08.2017 22:27

One question, how can we get to a new line if the text is in another line in the FILE?

Ответить
@jeremiahnji6
@jeremiahnji6 - 07.09.2017 07:34

Thank you for the upload I was successfully able to read and open a file of mine. I had one question. When I put my txt file in the same package of my program it does not read it. Only way I can access it is if I specify path. Is there something i need to do to allow my program to pick up the file (read it) from within my package?

Ответить
@kevinandrew1978
@kevinandrew1978 - 01.10.2017 16:24

im doing a coputer project on banking n i thought using file would be most useful as i would be able to input information n take out info n alter it according to he user's wishas in deposit /withdraw cash,bt im only in 10th grade so im gonna have to learn how to use file .i think im making gr8 progress with ur vids!!

Ответить
@AGO19Streetbear
@AGO19Streetbear - 27.10.2017 07:36

great video

Ответить
@dave_by_day7632
@dave_by_day7632 - 04.05.2018 06:23

Still can't believe java requires this many lines of code to do something so common, but it does

Ответить
@RyanKelly7373
@RyanKelly7373 - 18.05.2018 05:07

The While loop has an error that makes the BufferedReader actually iterate through each line 2 times before doing anything with the String values. By calling "BufferedReader.readLine() != null" you are effectively skipping 1 line before the important processes are finished.

Ответить
@rongal5830
@rongal5830 - 28.05.2018 19:17

Superb

Ответить
@saswatibhattacharjee7387
@saswatibhattacharjee7387 - 29.05.2018 11:56

What if my text file content 3rows and 3colmns and I want to read only the 2nd column ?
1 4 6
2 9 5
7 8 2

I want to read only ( 4 9 8)
Then how could I proceed?

Ответить
@karomahalmunawar5943
@karomahalmunawar5943 - 20.06.2018 00:17

thank you it helps my homework

Ответить
@Yaswanth_Rayana
@Yaswanth_Rayana - 30.07.2018 21:19

how can i use delay function to read the text file letter by letter

Ответить
@ritamr2690
@ritamr2690 - 01.08.2018 04:39

Does anybody have problems with reader. The problem coming in the console is: Exception in thread "main" java.lang.Error: Unresolved compilation problems:
reader cannot be resolved
reader cannot be resolved

at fileNames.main(fileNames.java:10)

Ответить
@binodgautam1620
@binodgautam1620 - 05.09.2018 18:58

how to display data in another window like in jframe?

Ответить
@TONGKING777
@TONGKING777 - 06.10.2018 05:15

System.out.println(text);
reader.close();
maybe you should that

Ответить
@popoporifox4229
@popoporifox4229 - 24.10.2018 10:33

How do I want the program to write on the next line of the text file?

Ответить
@makarioss
@makarioss - 24.11.2018 14:05

thanks a lot for the video. it was really helpful!!

Ответить
@marcocuentas6784
@marcocuentas6784 - 14.02.2019 21:08

any way to format the text so it spaces to a new line instead of run on sentence?

Ответить
@gerarddraadjer
@gerarddraadjer - 26.03.2019 16:05

Hi Brandon. Somehow I get the following error: "Could not find or load main class ReadingTextFromFiles". I'm doing exactly the same as you in the vid. Any suggestions how to fix this and why I get this error?

Ответить
@sohaibahmadkhan7423
@sohaibahmadkhan7423 - 25.05.2019 19:00

Sir, may you please do a tutorial on how to read from file recursively in java?

Ответить
@naulyd
@naulyd - 24.09.2019 01:39

Thank you for zooming in!!!!!

Ответить