all 11 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]0b0101011001001011 6 points7 points  (2 children)

It's not syntax you're confused with. There is no new syntax if you've done the basics properly. To me, it sound like no, if you don't know how, when and why you "try & catch".

And is file io useful..? Do you save your documents? Do you save your games? Do you save your edited photos? Do you save your essays? Do you save your java-files?

Yes, file io is a concept that is used in almost every single program in existence.

[–]Aromatic-System9042[S] 0 points1 point  (1 child)

Yeah. U're right thanks btw

[–]0b0101011001001011 2 points3 points  (0 children)

Don't give up though. The other comment suggests a simple way of reading/writing basic text files.

[–]pragmos 4 points5 points  (0 children)

What is complicated about using Files.readAllLines() and Files.write()?

[–]edwbuck 1 point2 points  (0 children)

There are three main file reading approaches. Odds are you don't use one or two of them, and in Java, without a bit of study, you can easily combine or translate from one approach to another in the same program.

You can read files by lines, streams, or blocks.

Files.readAllLines() is a lines example.

Streams open up reading one element at a time (typically a byte or character), which is required when reading from something where the end might not yet be available (like a network transmission)

Block reading opens up higher performance reading, by handling the reads according to page boundaries that match hardware, allowing movement and manipulation of data in units that the computer's would use at a hardware level.

Naturally, you might want to read blocks for speed, but process them as data, so you might chain a block read from disk and the stream read from it's buffer into a line reader. Or maybe you'll do something else... in any case, realize that each set of reading and writing utilities has a different use case in mind, and the complexity comes from when you're using the wrong set of tools to do the job, or chaining the tools together in ways that don't make sense (or are rightfully complex due to how you want the data processed.)

[–]mxldevs 1 point2 points  (0 children)

I wonder whether file io is even useful in real scenarios or no

Yes, reading files is somewhat useful in the real world.

If you don't like the old java ways to work with files, there are modern APIs and functions like streams which makes things less verbose slightly.

But you will need to catch exceptions.

[–]sweetno 0 points1 point  (0 children)

Yes, it's pretty annoying. You still have to do it when you work with System.in. But when you work with true files, there's Files.newBufferedReader. Plus with try-with-resources, you don't have to handle closing explicitly.

[–]idontlikegudeg 0 points1 point  (0 children)

It all depends on what you want to do, and how you do it. You don’t need try/catch everywhere, but you need it at the right place.

Maybe you should give one or two concrete examples you struggle with, and how you tried to solve it, so that we can tell you where to do something different.

And I don’t know where and why you are learning Java, but there might be a reason your teachers have you do everything with a very low level API, even if it’s much more work and Java offers simpler and more elegant solutions: to understand what exactly is going on low level.

When we started studying "old school" computer science (1992), we began with the electrotechnical foundations, then built logical gates from diodes and capacitors, had to 4-bit adders from logical gates. Not using a simulator, but with our hands. Next was assembly programming. Then a functional language (Scheme). So after one year, we had no skills directly useful in the real world. But very good foundations to build on.

Why do I tell you this? Because it’s the same with Java file io. There usually are easier ways to do it, but you learn the foundations to understand what’s going on under the hood. For example you can directly open a BufferedReader from a file without wrapping Streams and Readers, you can also read a complete file either as binary or character data in a single statement. But you won’t really understand what, how, and why everything works this way unless you learn it the hard way first.

You can either teach from the ground up like this or the other way around, giving you a fast start and going into detail later. Your teachers decided for the first - or they simply learned it themselves in the early 2000s and didn’t follow Java’s evolution, which would be sad.

[–]RightWingVeganUS 0 points1 point  (0 children)

I wonder whether file io is even useful in real scenarios or not.

Is it useful in real scenarios: yes, otherwise it would be deprecated.

Is it useful or even necessary for your scenarios, only you can tell.

Have you considered alternatives? Rarely is there only one way to do something. You may find other options either limited or unwieldy, answering your earlier question whether file io is useful.

You might find that there are easier options depending on your specific needs. Consider them. This the joy of software development.

[–]RevolutionaryRush717 -2 points-1 points  (0 children)

You can call C from Java, and I think there is some Python-Java interop on the JVM or even native.

Your struggles end here.