Scala file io library




















Similar to this answer , here is an example with fs2 version 1. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to write to a file in Scala? Ask Question. Asked 11 years ago. Active 2 years, 4 months ago. Viewed k times. Peter Mortensen If you know how to do so in Java then you can use the same in Scala. Is your question specifically with the Scala standard library? This library is really good: github.

Lihaoyi's OS-Lib library github. Add a comment. Active Oldest Votes. The code goes like so: def printToFile f: java. File op: java. Ngoc Dao 1, 3 3 gold badges 18 18 silver badges 26 26 bronze badges. Rex Kerr Rex Kerr k 26 26 gold badges silver badges bronze badges.

PrintWriter uses the platform default encoding, which probably means that the result file is not very portable. For example, if you want to produce a file that you can later send around by email, you should probably use the PrintWriter constructor that allows you to specify an encoding. JonaChristopherSahnwaldt - Sure, in special cases you might want to specify the encoding.

The default for the platform is the most sensible default on average. Same as with Source default encoding by default. You can of course add e. RexKerr - I disagree. One should specify the encoding in almost all cases. Most encoding errors I encounter happen because people don't understand or don't think about encoding.

They use the default and don't even know it because too many APIs let them get away with it. Nowadays, the most sensible default would probably be UTF Lucky you. I live in Germany and had to fix more broken umlauts than I care to remember.

JonaChristopherSahnwaldt - This is a reason to have a sensible default encoding, not to force everyone to specify it all the time. But if you're on a Mac and your files written by Java are gobbledygook because they aren't Mac OS Roman encoded, I'm not sure it's doing more good than harm. I think it's the platforms' fault that they haven't agreed on a charset. As an individual developer, typing in a string is really not going to solve the problem. All developers agreeing on UTF-8 would, but then that can just go in as the default.

Can't use a hammer, maybe a hole punch? Or are they already holes that need filling, maybe this guy can help youtube. Show 2 more comments. There are two main components of Scala IO: Core - Core primarily deals with Reading and writing data to and from arbitrary sources and sinks.

UTF8 output. VonC VonC 1. What about a Scala 2. The scalax project seems dead no commits since June Is this right? Eduardo: I have completed my answer with the new place for scala-io library which has been updated for Scala2. Is this really the current suggestion for Scala 2. Use Scala IO? There isn't anything in core Scala yet?

I've never used scalax. Mixing methods for character and binary data in one interface makes little sense and will very likely lead to encoding bugs that are hard to find.

The design of java. Similar to the answer by Rex Kerr, but more generic. Jus12 Jus12 Don't get me wrong, I like your code and it is very educational, but the more I see such constructs for simple problems, the more it reminds me about old "hello world" joke: ariel.

If you're writing one-liners, nothing at all matters. If you're writing significant programs large with an ongoing need for maintenance and evolution , this kind of thinking leads to the most rapid and pernicious kind of software quality degradation. Not everyone is going to have "scala eyes" until some level of practice -- it is funny to see this code example is coming from "Beginning" Scala — asyncwait.

I suppose than I was a step before "beginner" lol :D The problem is less the Scala tricks here, but the verbosity and poor style. I've edited this to much more readable.

After my refactor it's just 4 lines well, 4 with IDE line lengths, used 6 here to fit in the screen. The managed method from the ARM library makes sure that the resource is closed automatically when the resource goes out of scope. The ARM website shows several other ways the library can be used.

A second way to demonstrate the Loan Pattern is with the using method described on the Loan Pattern website. Both the ARM library and the using method end up with the same result, implementing the Loan Pattern to make sure your resource is closed automatically. The following code demonstrates how the fromFile method can be used with using to create a method that returns the entire contents of a file as a List[String] , wrapped in an Option :.

It can be used in the following ways:. If the process of opening and reading a file fails, you may prefer to return a Try or an empty List[String]. See Recipes The benefit of using Try is that you can get the cause of the exception back when you call this method and an exception occurs. In the Success case you handle the condition where the file was opened properly, and in the Failure case you deal with the exception however you want to.

In Scala 2. For instance, you can specify an expected character encoding for a file like this:. See the Scaladoc for the scala. For reading a file, we have created a test file with below content. Here is a simple program where we are using Scala Source class to read file data to a String and then split it using regular expression to a Map.

Finally we are printing the count of JournalDev in the file content. Word count line by line : Sometimes there arises a need to process each line rather than the whole contents of the file. This can be achieved through the getLines method.

For example below code;. So both the lines in above code snippet are doing the same thing. Below is a simple program showing how to write files in scala. This will produce a file Write. But I am wondering how to append new line of statement in an existing file. Hi Pankaj , yours all tutorials are excellent. Thank you so much.

Your email address will not be published. Prev Scala Extractors apply, unapply and pattern matching.



0コメント

  • 1000 / 1000