How do I save a String to a text file using Java?

If you’re simply outputting text, rather than any binary data, the following will work: PrintWriter out = new PrintWriter(“filename.txt”); Then, write your String to it, just like you would to any output stream: out.println(text); You’ll need exception handling, as ever. Be sure to call out.close() when you’ve finished writing. If you are using Java 7 … Read more

Print string to text file

It is strongly advised to use a context manager. As an advantage, it is made sure the file is always closed, no matter what: with open(“Output.txt”, “w”) as text_file: text_file.write(“Purchase Amount: %s” % TotalAmount) This is the explicit version (but always remember, the context manager version from above should be preferred): text_file = open(“Output.txt”, “w”) … Read more

How can I open multiple files using “with open” in Python?

As of Python 2.7 (or 3.1 respectively) you can write with open(‘a’, ‘w’) as a, open(‘b’, ‘w’) as b: do_something() (Historical note: In earlier versions of Python, you can sometimes use contextlib.nested() to nest context managers. This won’t work as expected for opening multiples files, though — see the linked documentation for details.) In the … Read more

UnicodeDecodeError: ‘charmap’ codec can’t decode byte X in position Y: character maps to

The file in question is not using the CP1252 encoding. It’s using another encoding. Which one you have to figure out yourself. Common ones are Latin-1 and UTF-8. Since 0x90 doesn’t actually mean anything in Latin-1, UTF-8 (where 0x90 is a continuation byte) is more likely. You specify the encoding when you open the file: … Read more

Writing a list to a file with Python, with newlines

Use a loop: with open(‘your_file.txt’, ‘w’) as f: for line in lines: f.write(f”{line}\n”) For Python <3.6: with open(‘your_file.txt’, ‘w’) as f: for line in lines: f.write(“%s\n” % line) For Python 2, one may also use: with open(‘your_file.txt’, ‘w’) as f: for line in lines: print >> f, line If you’re keen on a single function … Read more

File to byte[] in Java

From JDK 7 you can use Files.readAllBytes(Path). Example: import java.io.File; import java.nio.file.Files; File file; // …(file is initialised)… byte[] fileContent = Files.readAllBytes(file.toPath());

Is there a way to check if a file is in use?

Updated NOTE on this solution: Checking with FileAccess.ReadWrite will fail for Read-Only files so the solution has been modified to check with FileAccess.Read. ORIGINAL: I’ve used this code for the past several years, and I haven’t had any issues with it. Understand your hesitation about using exceptions, but you can’t avoid them all of the … Read more

How can I read a large text file line by line using Java?

A common pattern is to use try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { // process the line. } } You can read the data faster if you assume there is no character encoding. e.g. ASCII-7 but it won’t make much difference. It is highly likely … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)