Add a new line of text to an existing file in scala - scala

I am new to the Scala. I want to add a new line of text to an existing file.
I have tried the code below, but it is overwriting the existing text:
println("plese enter the text")
val text = Console.readLine()
val write = new PrintWriter(new File("Test.txt"))
write.write(text)
write.close()
please help me with this.

This is a java api question
You can do
val write = new PrintWriter(new FileOutputStream(new File("Test.txt"),true)))
This will open the file in append mode and not overwrite mode.
Documentation is here

Related

Create a text file on Azure using Scala

I have the following piece of scala code that will write text to a txt file sitting locally.
// PrintWriter
import java.io._
val pw = new PrintWriter(new File("resources/myfile.txt"))
pw.write("Test text")
pw.close
How do i get this to work on azure blob storage?
I have tried:
val pw = new PrintWriter(new File("wasb://[my container name]#[My Storage account].blob.core.windows.net/resources/myfile.txt"))
But it doesn't work.
What am i doing wrong? By the way, for the sake of this example, I'm keeping it simple. In reality, I am outputting more meaningful data.
Thanks
Con
You cannot create a file on Azure blog storage using the java.io or nio package. You need to use their REST API or their SDK.
https://learn.microsoft.com/en-us/rest/api/storageservices/create-file
https://learn.microsoft.com/en-us/azure/storage/blobs/storage-java-how-to-use-blob-storage

Adding to an existing zip file in scala

I have a zip file in hdfs and i need to add a file to the zip and save in the same HDFS location. Any examples would be appreciated.
I have the following code.
val filePattern = s"${hdfsFolderPath}/${filePath}.txt"
val zipFilePath = hdfsWrapper.getFileNameFromPattern(s"${targetFilePath}/*.zip")
if (hdfsWrapper.filter(filePattern).size() > 0)
{
Try
{
val zipEntry = new ZipEntry(filePattern)
val zos: ZipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath))
zos.putNextEntry(zipEntry)
zos.closeEntry()
zos.close()
}
}
Would like to know if above code is right?
I believe that your code would result in the zip file being replaced with a new one containing just the new file. See Appending files to a zip file with Java for an example of adding a file to a zip archive.
I'm not extremely familiar with HDFS, but I suspect that you can't write to that directly either, you probably have to create the new zip file and then replace it in HDFS

read .doc file using scala

I want to read .doc file in scala. I tried using apache.poi library for this but the method HWPFDocument(java.io.InputStream istream) accepts java io stream.
If anyone can shed some light on this, that would great!
So, here is a teaser to get you started:
val fis = new FileInputStream("/path/to/file/doc.doc")
val doc = new HWPFDocument(fis)
val we = new WordExtractor(doc)
val paras = we.getParagraphText()
You can use InputStream in Scala, just as any other Java class/interface.

File IO not working in eclipse

I have a FileWriter to write a text to a file. The file is not getting created at all. I've checked the current working directory. Moreover, when I debug, the code for writing the text into the file is not getting executed at all...Have anyone experienced anything similar?
File ff = new File("ffile.txt");
FileWriter frr = new FileWriter(ff);
frr.write("hello");
frr.close();
to create new file use createNewFile();
File ff = new File("ffile.txt");
ff.createNewFile();
FileWriter frr = new FileWriter(ff);
frr.write("hello");
frr.close();

Can't write into a .txt file in netbeans

I have a file called SAVE.txt. It is in the same package as the class k. The problem is I can't write anything in the .txt file using the following code inside k:
File saveButton = new File ("SAVE.txt");
BufferedWriter output = new BufferedWriter (new FileWriter (saveButton));
output.write("something");
output.close();
Can anyone help me with this?
bw = new BufferedWriter(new FileWriter("filepath",true));
bw.write("Hello World!");
bw.write("\n");
bw.write("Hello World 2 !\n");
bw.write("Hello World 3 !" + "\n");
bw.close();
Try this?
Did you try something easy like this:
FileWriter f = new FileWriter("test.txt");
f.write("hello");
f.close();
When you write new File ("SAVE.txt"), since you specified a relative path, it refers to a file SAVE.txt in the current working directory. The current directory is in general completely separate from the directory corresponding to your Java package.
When you run code in Netbeans, it should be possible to specify the working directory (look in the project settings). Set it to some well-defined location, like the root of your project. Now specify the path relative to that working directory. For example, you could use new File ("out/SAVE.txt").