text file to console directly - java

Was thinking of making a program,such that whatsoever I write on a text file ,it should come on the console
made this program
import java.io.*;
class Redirector
{
public static void main(String[] args) {
try
{
if(args.length!=1)
{
throw(new Exception("wrong way"));
}
System.setIn(new FileInputStream("b.txt"));
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
PrintStream x=new PrintStream(new FileOutputStream(args[0]));
System.out.println("enter text,end to terminanate");
while(true)
{
String str=r.readLine();
if(!str.equalsIgnoreCase("end"))
System.out.println(str);
else
break;
}
System.out.println("done");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
but the problem is that I want the whatsoever I write dynamically on the file and press enter should come on the console,whereas in this case it should be saved

What you're asking for is not possible. As long as you don't save the text file, its contents are not actually on the disk, they're only in memory.
You could theoretically try to access it in the allocated memory of whatever program you're using to edit the text file, but this is very difficult as it would require knowing the layout of its memory chunk. Moreover, it is a huge security risk, so most modern operating systems will make very sure you can't do it.

Related

Unexpected amount of lines when writing to a csv file

A part of my application writes data to a .csv file in the following way:
public class ExampleWriter {
public static final int COUNT = 10_000;
public static final String FILE = "test.csv";
public static void main(String[] args) throws Exception {
try (OutputStream os = new FileOutputStream(FILE)){
os.write(239);
os.write(187);
os.write(191);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
for (int i = 0; i < COUNT; i++) {
writer.write(Integer.toString(i));
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(checkLineCount(COUNT, new File(FILE)));
}
public static String checkLineCount(int expectedLineCount, File file) throws Exception {
BufferedReader expectedReader = new BufferedReader(new FileReader(file));
try {
int lineCount = 0;
while (expectedReader.readLine() != null) {
lineCount++;
}
if (expectedLineCount == lineCount) {
return "correct";
} else {
return "incorrect";
}
}
finally {
expectedReader.close();
}
}
}
The file will be opened in excel and all kind of languages are present in the data. The os.write parts are for prefixing the file with a byte order mark as to enable all kinds of characters.
Somehow the amount of lines in the file do not match the count in the loop and I can not figure out how. Any help on what I am doing wrong here would be greatly appreciated.
You simply need to flush and close your output stream (forcing fsync) before opening the file for input and counting. Try adding:
writer.flush();
writer.close();
inside your try-block. after the for-loop in the main method.
(As a side note).
Note that using a BOM is optional, and (in many cases) reduces the portability of your files (because not all consuming app's are able to handle it well). It does not guarantee that the file has the advertised character encoding. So i would recommend to remove the BOM. When using Excel, just select the file and and choose UTF-8 as encoding.
You are not flushing the stream,Refer oracle docs for more info
which says that
Flushes this output stream and forces any buffered output bytes to be
written out. The general contract of flush is that calling it is an
indication that, if any bytes previously written have been buffered by
the implementation of the output stream, such bytes should immediately
be written to their intended destination. If the intended destination
of this stream is an abstraction provided by the underlying operating
system, for example a file, then flushing the stream guarantees only
that bytes previously written to the stream are passed to the
operating system for writing; it does not guarantee that they are
actually written to a physical device such as a disk drive.
The flush method of OutputStream does nothing.
You need to flush as well as close the stream. There are 2 ways
manually call close() and flush().
use try with resource
As I can see from your code that you have already implemented try with resource and also BufferedReader class also implements Closeable, Flushable so use code as per below
public static void main(String[] args) throws Exception {
try (OutputStream os = new FileOutputStream(FILE); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8))){
os.write(239);
os.write(187);
os.write(191);
for (int i = 0; i < COUNT; i++) {
writer.write(Integer.toString(i));
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(checkLineCount(COUNT, new File(FILE)));
}
When COUNT is 1, the code in main() will write a file with two lines, a line with data plus an empty line afterwards. Then you call checkLineCount(COUNT, file) expecting that it will return 1 but it returns 2 because the file has actually two lines.
Therefore if you want the counter to match you must not write a new line after the last line.
(As another side note).
Notice that writing CSV-files the way you are doing is really bad practice. CSV is not so easy as it may look at first sight! So, unless you really know what you are doing (so being aware of all CSV quirks), use a library!

System.out.println prints the string but System.out.print does not

I am trying to store the words in a file separated by coma in a java array
The file is
Age,Income,Student,Credit Rating,Class: Buys Computer
Youth,high,No,Fair,No
Youth,high,No,Excellent,No
Middle aged,high,No,Excellent,No
Senior,medium,No,Fair,Yes
Senior,Low,Yes,Fair,Yes
Senior,Low,Yes,Excellent,No
public class Test {
public static void main(String args[]) throws FileNotFoundException, IOException{
FileInputStream f=new FileInputStream("F:\\pr\\src\\dmexam\\inp2.txt");
int size,nr=7,nc=5,j=0,i=0;
char ch;
String table[][]=new String[nr][nc];
size=f.available();
table[0][0]=new String();
while(size--!=0){
ch=(char)f.read();
if(ch=='\n')
{
i++;
if(i>=nr)
break;
table[i][0]=new String();
j=0;
continue;
}
if(ch==',')
{
j++;
table[i][j]=new String();
continue;
}
table[i][j]+=ch;
}
f.close();
System.out.println("The given table is:::---");
for(i=0;i<nr;i++){
for(j=0;j<nc;j++){
System.out.print(" "+table[i][j]);
System.out.print(" ");
}
}
}
}
But the output is
The given table is:::---
But if the for is changed like this
System.out.println("The given table is:::---");
for(i=0;i<nr;i++){
for(j=0;j<nc-1;j++){
System.out.print(" "+table[i][j]);
System.out.print(" ");
}
System.out.println(table[i][nc-1]);
}
The output is
The given table is:::---
Age Income Student Credit Rating Class: Buys Computer
Youth high No Fair No
Youth high No Excellent No
Middle aged high No Excellent No
Senior medium No Fair Yes
Senior Low Yes Fair Yes
Senior Low Yes Excellent No
I want to know "why System.out.print is not workig???"...
The PrintStream that System.out uses has an internal buffer, since writing to stdout is relatively expensive -- you wouldn't necessarily want to do it for each character. That buffer is automatically flushed when you write a newline, which is why println causes the text to appear. Without that newline, your string just sits in the buffer, waiting to get flushed.
You can force a manual flush by invoking System.out.flush().
Okay let me try to help you out here. So you are making your life really rough at the moment. Have you tried to look at different libraries like BufferedWritter/FileWritter?
You can easily import these into your project using:
import java.io.BufferedWritter;
import java.io.FileWritter;
It is also recommended to catch errors using the IOException library:
import java.io.IOException;
As for the separation of the words, these libraries give you tools like control over the delimiter. For example we can do something like this:
//this is if you are creating a new file, if not, you want true to append to an existing file
BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt", boolean false));
try
{
// write the text string to the file
bw.write("Youth,high,No,Fair,No");
// creates a newline in the file
bw.newLine();
}
// handle exceptions
catch (IOException exc)
{
exc.printStackTrace();
}
// remember to close the file at the end
bw.close();
Now that is for hard coding the data, but we can do this with a for loop. We can add delimiters in the function within the for loop, for example: (I am not sure how you have the data stored, but I am assuming you save it in an array. I am also assuming there will ALWAYS be 5 sets of data per line)
BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt", boolean false));
for (int i = 1, i <= listName.size()+1, i++) {
if (i % 5 == 0) {
bw.write(listName.get(i-1));
bw.write(", ");
bw.newLine();
} else {
bw.write(listName.get(i-1));
bw.write(", ");
}
}
This would write to the file:
Youth,high,No,Fair,No
Youth,high,No,Excellent,No
Middle aged,high,No,Excellent,No
Senior,medium,No,Fair,Yes
Senior,Low,Yes,Fair,Yes
Senior,Low,Yes,Excellent,No
This may make your life a little easier (if I am understanding your needs clearly). Next time please make sure to flesh out your question more than you did.
DISCLAIMER: I did not test all of the code, so if you find an error please let me know and I will edit it as needed. If I get time I will make sure it works.

Creating commands for a terminal app in Java

I'm new to programming, and I'm making an app that only runs in the command-line. I found that I could use a BufferedReader to read the inputs from the command-line.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String Input = "";
while (Input.equalsIgnoreCase("Stop") == false) {
Input = in.readLine();
//Here comes the tricky part
}
in.close();
What I'm trying to do now is to find a way to create different "commands" that you could use just by typing them in the command-line. But these commands might have to be used multiple times. Do I have to use some kind of Command design pattern with a huge switch statement (that doesn't seem right to me)? I'd like to avoid using an extra library.
Can someone with a bit more experience that me try to help me?
You could try something like this:
public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = "";
try {
while (!input.equalsIgnoreCase("stop")) {
showMenu();
input = in.readLine();
if(input.equals("1")) {
//do something
}
else if(input.equals("2")) {
//do something else
}
else if(input.equals("3")) {
// do something else
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void showMenu() {
System.out.println("Enter 1, 2, 3, or \"stop\" to exit");
}
It is good practice to keep your variables lower cased.
I would also say that !Input.equalsIgnoreCase("stop") is much more readable than Input.equalsIgnoreCase("stop") == false although both are logically equivalent.
If it's just about reading the program parameters you can just add them behind the Java application call and access them through your args argument of your main method. And then you can loop through the array and search for the flags your program accepts.

Java: how to synchronize file modification by threads

Only one instance of my Java application can run at a time. It runs on Linux. I need to ensure that one thread doesn't modify the file while the other thread is using it.
I don't know which file locking or synchronization method to use. I have never done file locking in Java and I don't have much Java or programming experience.
I looked into java NIO and I read that "File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine." Right away I knew that I needed expert help because this is production code and I have almost no idea what I'm doing (and I have to get it done today).
Here's a brief outline of my code to upload some stuff (archive files) to a server. It gets the list of files to upload from a file (call it "listFile") -- and listFile can be modified while this method is reading from it. I minimize the chances of that by copying listFile to a temp file and using that temp file thereafter. But I think I need to lock the file during this copy process (or something like that).
package myPackage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import com.example.my.FileHelper;
import com.example.my.Logger;
public class BatchUploader implements Runnable {
private int processUploads() {
File myFileToUpload;
File copyOfListFile = null;
try {
copyOfListFile = new File("/path/to/temp/workfile");
File origFile = new File("/path/to/listFile"); //"listFile" - the file that contains a list of files to upload
DataWriter.copyFile(origFile, copyOfListFile);//see code below
} catch (IOException ex) {
Logger.log(ex);
}
try {
BufferedReader input = new BufferedReader(new FileReader(copyOfListFile));
try {
while (!stopRunning && (fileToUploadName = input.readLine()) != null) {
upload(new File(fileToUploadName));
}
} finally {
input.close();
isUploading = false;
}
}
return filesUploadedCount;
}
}
Here is the code that modifies the list of files to be uploaded used in the above code:
public class DataWriter {
public void modifyListOfFilesToUpload(String uploadedFilename) {
StringBuilder content = new StringBuilder();
try {
File listOfFiles = new File("/path/to/listFile"); //file that contains a list of files to upload
if (!listOfFiles.exists()) {
//some code
}
BufferedReader input = new BufferedReader(new FileReader(listOfFiles));
try {
String line = "";
while ((line = input.readLine()) != null) {
if (!line.isEmpty() && line.endsWith(FILE_EXTENSION)) {
if (!line.contains(uploadedFilename)) {
content.append(String.format("%1$s%n", line));
} else {
//some code
}
} else {
//some code
}
}
} finally {
input.close();
}
this.write("/path/to/", "listFile", content.toString(), false, false, false);
} catch (IOException ex) {
Logger.debug("Error reading/writing uploads logfile: " + ex.getMessage());
}
}
public static void copyFile(File in, File out) throws IOException {
FileChannel inChannel = new FileInputStream(in).getChannel();
FileChannel outChannel = new FileOutputStream(out).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
throw e;
} finally {
if (inChannel != null) {
inChannel.close();
}
if (outChannel != null) {
outChannel.close();
}
}
}
private void write(String path, String fileName, String data, boolean append, boolean addNewLine, boolean doLog) {
try {
File file = FileHelper.getFile(fileName, path);
BufferedWriter bw = new BufferedWriter(new FileWriter(file, append));
bw.write(data);
if (addNewLine) {
bw.newLine();
}
bw.flush();
bw.close();
if (doLog) {
Logger.debug(String.format("Wrote %1$s%2$s", path, fileName));
}
} catch (java.lang.Exception ex) {
Logger.log(ex);
}
}
}
My I suggest a slightly different approach. Afair on Linux the file rename (mv) operation is atomic on local disks. No chance for one process to see a 'half written' file.
Let XXX be a sequence number with three (or more) digits. You could let your DataWriter append to a file called listFile-XXX.prepare and write a fixed number N of filenames into it. When N names are written, close the file and rename it (atomic, see above) to listFile-XXX. With the next filename, start writing to listFile-YYY where YYY=XXX+1.
Your BatchUploader may at any time check whether it finds files matching the pattern listFile-XXX, open them, read them upload the named files, close and delete them. There is no chance for the threads to mess up each other's file.
Implementation hints:
Make sure to use a polling mechanism in BatchUploader that waits 1 or more seconds if it does not find a file ready for upload (prevent idle wait).
You may want to make sure to sort the listFile-XXX according to XXX to make sure the uploading is kept in sequence.
Of course you could variate the protocol of when listFile-XXX.prepare is closed. If DataWriter has nothing to do for a longer time, you don't want to have files ready for upload hang around just because there are not yet N ready.
Benefits: no locking (which will be a pain to get right), no copying, easy overview over the work queue and it state in the file system.
Here is a slightly different suggestion. Assuming your file names don't have '\n' characters in them (it's a big assumption on linux, I know, but you can have your writer look up for that), why not only read complete lines and ignore the incomplete ones? By incomplete lines, I mean lines that end with EOF and not with \n.
Edit: see more suggestions in comments below.

Writing to one file from two threads produces unexpected result

When I run this test
public class Test extends Thread {
String str;
Test(String s) {
this.str = s;
}
#Override
public void run() {
try {
FileWriter fw = new FileWriter("1.txt", true);
for (char c : str.toCharArray()) {
System.out.print(c);
fw.write(c);
}
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
new File("1.txt").delete();
new Test("11111111111111111111").start();
new Test("22222222222222222222").start();
}
}
it shows exactly how it writes characters to 1.txt
2222222222222222111211111211111121211111
but in 1.txt I see a different result
2222222222222222222211111111111111111111
why is that?
Intermediate buffers. Usually modern OS buffer file writes to write full sectors at once, to avoid too much hard-drive header seeks, allow use of DMA techniques, etc...
This may be an example of ASYNC I/O Write.
The kernel updates the corresponding process(different) pages in the page-cache and marks them dirty(needs to be updated in HDD). Then the control quickly returns to the corresponding process (here 2 different processes) which can continue to run and update in the console in the order called by the scheduler. The data is flushed to HDD later at a more optimal time(low cpu-load) in a more optimal way(sequentially bunched writes). and hence writes from process area sequentially.

Categories