Outputing text to a file? - java

After running my program I get this beautifully formatted text:
What do I need add to my code in order to get this text outputted to a .txt file exactly as is?

Assuming you're currently writing your output to the screen using something like System.out.Println(...), a comparable way to write text to a file is to use a PrintStream.
You should be able to find many examples of how to do that if you search; here's one.
import java.io.*;
class PrintStreamDemo {
public static void main(String args[]){
FileOutputStream out;
PrintStream ps; // declare a print stream object
try {
// Create a new file output stream
out = new FileOutputStream("myfile.txt");
// Connect print stream to the output stream
ps = new PrintStream(out);
ps.println ("This data is written to a file:");
System.err.println ("Write successfully");
ps.close();
}
catch (Exception e){
System.err.println ("Error in writing to file");
}
}
}
ps. one caution: the "beautifully formatted" part of your text probably relies on the output being displayed in a monospaced font (all characters the same width); if viewed in a non-monospaced font, the columns won't line up. If you're saving it as a plain .txt file, you don't have control over what font someone else will use to display that file when they open it.
Update:
There are a couple of approaches you could take, if you find you've got a program full of System.out.println calls and you want to direct the output to a file instead.
1) The quick & dirty way would be to open a PrintStream to your desired output file, then call System.setOut() to redirect System.out to the specified PrintStream.
2) A perhaps cleaner way would be to rewrite all the calls of System.out.println to use your own output method. That way when you want to change how you're handling output (for example, send it to multiple files, to both the screen and a file, or whatever), you have just one place to change. More work up front, but gives you more flexibility in the end.

You didnt clarify where data come from, but anyway input or db, I would place on a multidimensional array. Then print will be easy.
P.S. Also to keep format I would use tab separated values "/t"
public class Parser {
private void parseData{
int[][] array = new int[3][12];
// print array in rectangular form
for (int r=0; r<array.length; r++) {
for (int c=0; c<array[r].length; c++) {
System.out.print(array[r][c] + "\t");
}
System.out.println("");
}
}
}

Related

Reliable saving JTextArea string in multi entry text file

I have a software that stores its data in multible nested data objects. On saving this project data, every instance gets an out handle (BufferedWriter) and writes its own data. Most data is single line and no problem, but there are a few multiline strings that come from JTextAreas. For storing them, I wrote a sub method multiLineWriter(), that splits the string in single lines, writes the number of lines and then the single lines. In theory. Because its not always working. Often it writes out the line count as 1 but then writes out two lines. Or it writes out 1, but writes out two lines with text and an empty line. Its not reliable. After loading the project back, often the complete data is destroyed. A typcal object saving block looks like this:
// *** write data to file
public void writeDataFile(BufferedWriter out) {
try {
out.write(""+getHeadline() );
out.newLine();
out.write(""+getStartDateAsString() );
out.newLine();
out.write(""+getEndDateAsString() );
out.newLine();
out.write(""+getPlaceIndex() );
out.newLine();
multiLineWriter(out, getDescription() );
} catch(Exception e) {}
}
// *** read data from File
public void readDataFile(BufferedReader in) {
try {
setHeadline(in.readLine());
setStartDateAsString(in.readLine());
setEndDateAsString(in.readLine());
setPlaceIndex(in.readLine());
setDescription(multiLineReader(in));
} catch(Exception e) {}
}
The multline writer/reader looks like this:
public void multiLineWriter(BufferedWriter out, String areaText) {
try {
String ls = System.getProperty("line.separator");
String[] lines = areaText.split(ls);
int lineCount = lines.length;
out.write(""+lineCount);
out.newLine();
for(int i = 0;i<lineCount;i++) {
out.write(lines[i]);
out.newLine();
}
} catch(Exception e) {}
}
public String multiLineReader(BufferedReader in) {
String targetString = "";
try {
String ls = System.getProperty("line.separator");
int lineCount = Integer.parseInt(in.readLine());
for(int i = 0;i<lineCount;i++) {
targetString = targetString + in.readLine() + ls;
}
} catch(Exception e) {}
return targetString;
}
As said, lineCount often is 1, but the loop seems to go two or more times because I have sometimes two or three lines after the 1 in a datafile.
This is not reliable for the project. Do you have an idea how I can change the multiLineWriter/reader to reliably store and read the data? The JTextArea save method does not work in this combined data file format.
__More info: __
Properties are a good style for the whole datafile. Since I was allright with the old style seen above most of the times I am sticking to that. Changing the current project to properties is a lot of handwork.
I reuse the out. I have Project Object, that creates the out. This out is then passed to multiple objects with subobjects, sometimes in loops, and everyone writes it data to this single out. After all data is written the project Object of course flushes and closes the stream. The empty exceptions are no problem in this case, because there are no exceptions (so there is nothing to analyse in a stack trace). Its not an exception problem but a logical problem.
The JTextArea read/write is not a good option. At time of saving the file, the data is not in a JTextArea but in a string, that was saved sometime ago during runtime from a JTextArea. To use the write method of JtextArea I would need to restore the string to the area and then use the write method. Because of hundreds of those description objects I would need to do this hundred of times in a save process. Sounds not well. On the other hand I am sure that the read method would not work, because it would read in the datafile up to the end and wouldn't handle the nested datastructure in the datafile.
Its not bad to be human readable. Currently this is helping me, to manually correct the values after a save process, so I am not loosing any data (I now this is stupid, but it works:-)
To be short: I guess I have a problem with the split method of strings and the content of the strings in the string array.
Problem should be made clearer. I have this JTextArea. It is like one field in a display for datasets (its a little private genealogy program that mainly manages hundreds of persons and places). A lot of dataobjects have a description field. Contents of the JTextArea are stored to one single String variable when you change the person in display for example (String personDescription). The writeDataFile() Method you see above is for an event object, that has a description field, too.
So when I write a File, I write from one String to the file. Since this string is taken from the JTextArea, it contains all new line characters that you can produce in a JTextArea. When storing this with one out.write (data) call you have multiple lines in the resulting data file because of possible new line characters in the String. So you can't read all this content back with one in.readLine() call. That's why I created the multiline writers and readers. But they don't work as expected.
Here I show you an exerpt from the resulting datafile
...
# +++ FileCollection:
0
# +++ ImageCollection:
0
58
true
Surname
Arthur
25.09.1877
1
01.01.1950
6
https://familysearch.org/
1
Bekannt ist, dass er auf dem Friedhof Großbeerenstr. lag.
Bekannt ist auch, dass die Trauzeugen bei der Heirat Dorothea Surname und Hermann Surname waren. Hermann ist vermutlich ein Bruder von Valerie.
Weitere Informationen gibt es nicht bisher.
# +++ EventCollection:
0
# +++ FileCollection:
0
...
There is more data before and below, but here is the wrong written data. Its directly below the link to familysearch.org. The first line that follows should have the line count. If there is no text it would have a 0 and the next line would be the info sting '# + EventCollection:'. If there would be one line, it would have a 1 and the next line would be that single line of text for description. Or other numbers depending on the amount of lines from the JTextArea. But as you see, there is written a 1 in this case, but there are 3 (!) Lines of text following.
So the main problem seems to be the way I work with the split method in the multiLineWriter().
String ls = System.getProperty("line.separator");
String[] lines = areaText.split(ls);
int lineCount = lines.length;
This seems to be critical. Since I write the resulting array of the split in a loop, this loop must be done three times? Because I have 3 lines of text in the datafile. But the lineCount is written as a 1? So this seems to be wrong. Could be that this string was not splitted, but still contains line break characters. That would not be what I am looking for. And in the array of splittet Strings there should not be any line break characters anymore (that would destroy the file writing, too).
Hope the problem is better described now. And the question is, how should the multiline writer and reader method be designed to store and read this data reliable.
I tried it myself. As I said there was a problem using the split method on strings. I changed this now to use a Scanner. To be correct, I use some ideas from How do I use System.getProperty("line.separator").toString()?
So in the end I just changed the multiLineWrite Method to use the Scanner (from the util package). It looks like this:
public void multiLineWriter(BufferedWriter out, String areaText) {
List<String> slines = new ArrayList<String>();
try {
Scanner sc = new Scanner(areaText);
while (sc.hasNextLine()) {
slines.add(sc.nextLine());
}
int slineCount = slines.size();
out.write(""+slineCount);
out.newLine();
for(int i = 0;i<slineCount;i++) {
out.write(slines.get(i));
out.newLine();
}
} catch(Exception e) {}
}
So now this seems to be reliable for me. I did a test with parallel writing of the split method and the Scanner method, and the split method had the wrong line count and the Scanner was correct.

In Java, How to delete a row in a text file that meets search criteria?

public static String findAndDeleteStudent(String qStudent)
{
String fileName = "data/Students.txt";
String findNumber = qStudent;
File file = new File(fileName);
Scanner dataText = null;
String result="";
try
{
dataText = new Scanner(file);
while (dataText.hasNext())
{
String line = dataText.nextLine();
if (line.contains(findNumber))
{
//System.out.println(line);
//line.trim();
//System.out.println(items[1]);
System.out.println(line);
result ="done";
}
}
}
catch (NumberFormatException nfe)
{
result = "error";
}
catch (NoSuchElementException nsf)
{
result = "error";
}
catch (FileNotFoundException fnf)
{
result = "error";
}
return result;
}
I want to know the shortest way to delete the line string without re-writing the whole text file without the line string (that i want to delete).
I am able to retrieve the line as a string from the file using above code.
but can't delete. please help.
If the file is small enough and you are bulk deleting, then you could always read into into a HashMap and run .remove(qStudent) (assuming the key is based on qStudent). Then, you can write out the HashMap to the file when finished with all deletes. If, however, you're only deleting one here and there, then yes, you'll have to do some form or (re)write. It is possible, though to do random access into a file if (1) each entry is the exact same size, (2) that size is known, and (3) there is a deterministic way to locate each student (unlikely, however). Then you can use a java.io.RandomAccessFile and its seek function to skip to the exact location. For larger files, if they are sorted, then you can use a RandomAccessFile and binary search to speed up locating the record in question. Also for large files, you can use memory mapping. The last two, however, will still require file (re)writing.
As MarsAtomic suggested, (and Hot Licks corrected) you can't remove bytes randomly from a file.
But there are two optimizations possible.
But there is no need to completely rewrite the file.
If your line-to-be-deleted is halfway across the file, you don't need to touch the first half of the file. You would just need to rewrite second half of the file.
You can scan line by line and once you find the line, you can start copying.
But while copying, there is no need to go line by line, you can go for faster ways of copying by transferring large number of lines at once.
Hope this helps.

How to delete stuff printed to console by System.out.println()?

In a Java application I'm using some calls to System.out.println(). Now I want to find a way to programmatically delete this stuff.
I couldn't find any solution with google, so are there any hints?
You could print the backspace character \b as many times as the characters which were printed before.
System.out.print("hello");
Thread.sleep(1000); // Just to give the user a chance to see "hello".
System.out.print("\b\b\b\b\b");
System.out.print("world");
Note: this doesn't work flawlessly in Eclipse console in older releases before Mars (4.5). This works however perfectly fine in command console. See also How to get backspace \b to work in Eclipse's console?
Clearing screen in Java is not supported, but you can try some hacks to achieve this.
a) Use OS-depends command, like this for Windows:
Runtime.getRuntime().exec("cls");
b) Put bunch of new lines (this makes ilusion that screen is clear)
c) If you ever want to turn off System.out, you can try this:
System.setOut(new PrintStream(new OutputStream() {
#Override public void write(int b) throws IOException {}
}));
You could use cursor up to delete a line, and erase text, or simply overwrite with the old text with new text.
int count = 1;
System.out.print(String.format("\033[%dA",count)); // Move up
System.out.print("\033[2K"); // Erase line content
or clear screen
System.out.print(String.format("\033[2J"));
This is standard, but according to wikipedia the Windows console don't follow it.
Have a look: http://www.termsys.demon.co.uk/vtansi.htm
I am using blueJ for java programming. There is a way to clear the screen of it's terminal window. Try this:-
System.out.print ('\f');
this will clear whatever is printed before this line. But this does not work in command prompt.
System.out is a PrintStream, and in itself does not provide any way to modify what gets output. Depending on what is backing that object, you may or may not be able to modify it. For example, if you are redirecting System.out to a log file, you may be able to modify that file after the fact. If it's going straight to a console, the text will disappear once it reaches the top of the console's buffer, but there's no way to mess with it programmatically.
I'm not sure exactly what you're hoping to accomplish, but you may want to consider creating a proxy PrintStream to filter messages as they get output, instead of trying to remove them after the fact.
To clear the Output screen, you can simulate a real person pressing CTRL + L (which clears the output). You can achieve this by using the Robot() class, here is how you can do this:
try {
Robot robbie = new Robot();
robbie.keyPress(17); // Holds CTRL key.
robbie.keyPress(76); // Holds L key.
robbie.keyRelease(17); // Releases CTRL key.
robbie.keyRelease(76); // Releases L key.
} catch (AWTException ex) {
Logger.getLogger(LoginPage.class.getName()).log(Level.SEVERE, null, ex);
}
There are two different ways to clear the terminal in BlueJ. You can get BlueJ to automatically clear the terminal before every interactive method call. To do this, activate the 'Clear screen at method call' option in the 'Options' menu of the terminal. You can also clear the terminal programmatically from within your program. Printing a formfeed character (Unicode 000C) clears the BlueJ terminal, for example:
System.out.print('\u000C');
Just to add to BalusC's anwswer...
Invoking System.out.print("\b \b") repeatedly with a delay gives an exact same behavior as when we hit backspaces in {Windows 7 command console / Java 1.6}
I've found that in Eclipse Mars, if you can safely assume that the line you replace it with will be at least as long as the line you are erasing, simply printing '\r' (a carriage return) will allow your cursor to move back to the beginning of the line to overwrite any characters you see. I suppose if the new line is shorter, you can just make up the different with spaces.
This method is pretty handy in eclipse for live-updating progress percentages, such as in this code snippet I ripped out of one of my programs. It's part of a program to download media files from a website.
URL url=new URL(link);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.connect();
if(connection.getResponseCode()!=HttpURLConnection.HTTP_OK)
{
throw new RuntimeException("Response "+connection.getResponseCode()+": "+connection.getResponseMessage()+" on url "+link);
}
long fileLength=connection.getContentLengthLong();
File newFile=new File(ROOT_DIR,link.substring(link.lastIndexOf('/')));
try(InputStream input=connection.getInputStream();
OutputStream output=new FileOutputStream(newFile);)
{
byte[] buffer=new byte[4096];
int count=input.read(buffer);
long totalRead=count;
System.out.println("Writing "+url+" to "+newFile+" ("+fileLength+" bytes)");
System.out.printf("%.2f%%",((double)totalRead/(double)fileLength)*100.0);
while(count!=-1)
{
output.write(buffer,0,count);
count=input.read(buffer);
totalRead+=count;
System.out.printf("\r%.2f%%",((double)totalRead/(double)fileLength)*100.0);
}
System.out.println("\nFinished index "+INDEX);
}
The easiest ways to do this would be:
System.out.println("\f");
System.out.println("\u000c");
For intellij console the 0x08 character worked for me!
System.out.print((char) 8);
I found a solution for the wiping the console in an Eclipse IDE. It uses the Robot class. Please see code below and caption for explanation:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public void wipeConsole() throws AWTException{
Robot robbie = new Robot();
//shows the Console View
robbie.keyPress(KeyEvent.VK_ALT);
robbie.keyPress(KeyEvent.VK_SHIFT);
robbie.keyPress(KeyEvent.VK_Q);
robbie.keyRelease(KeyEvent.VK_ALT);
robbie.keyPress(KeyEvent.VK_SHIFT);
robbie.keyPress(KeyEvent.VK_Q);
robbie.keyPress(KeyEvent.VK_C);
robbie.keyRelease(KeyEvent.VK_C);
//clears the console
robbie.keyPress(KeyEvent.VK_SHIFT);
robbie.keyPress(KeyEvent.VK_F10);
robbie.keyRelease(KeyEvent.VK_SHIFT);
robbie.keyRelease(KeyEvent.VK_F10);
robbie.keyPress(KeyEvent.VK_R);
robbie.keyRelease(KeyEvent.VK_R);
}
Assuming you haven't changed the default hot key settings in Eclipse and import those java classes, this should work.
BalusC answer didn't work for me (bash console on Ubuntu). Some stuff remained at the end of the line. So I rolled over again with spaces. Thread.sleep() is used in the below snippet so you can see what's happening.
String foo = "the quick brown fox jumped over the fence";
System.out.printf(foo);
try {Thread.sleep(1000);} catch (InterruptedException e) {}
System.out.printf("%s", mul("\b", foo.length()));
try {Thread.sleep(1000);} catch (InterruptedException e) {}
System.out.printf("%s", mul(" ", foo.length()));
try {Thread.sleep(1000);} catch (InterruptedException e) {}
System.out.printf("%s", mul("\b", foo.length()));
where mul is a simple method defined as:
private static String mul(String s, int n) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < n ; i++)
builder.append(s);
return builder.toString();
}
(Guava's Strings class also provides a similar repeat method)
I have successfully used the following:
#Before
public void dontPrintExceptions() {
// get rid of the stack trace prints for expected exceptions
System.setErr(new PrintStream(new NullStream()));
}
NullStream lives in the import com.sun.tools.internal.xjc.util package so might not be available on all Java implementations, but it's just an OutputStream, should be simple enough to write your own.
this solution is applicable if you want to remove some System.out.println() output. It restricts that output to print on console and print other outputs.
PrintStream ps = System.out;
System.setOut(new PrintStream(new OutputStream() {
#Override
public void write(int b) throws IOException {}
}));
System.out.println("It will not print");
//To again enable it.
System.setOut(ps);
System.out.println("It will print");

Writing long string in Java to file

I'm trying to dump a large integer array (10 000 elements) into a text file but am encountering some problems. I've tried two different approaches and neither seems to be working. Below is the function I've written:
private static void writeToFile(String name, int[] a){
try {
FileWriter fw = new FileWriter(name);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("working");
for (int n: a){
bw.write(n + " ");
}
bw.close();
} catch (IOException e) {
System.err.print("Unable to write to file " + name+ ".");
e.printStackTrace();
}
}
The first thing I tried was creating a string in the for loop and then writing the whole string to the file. Neither method works and gives me the same results as follows:
File is created but left blank
Works fine for shorter arrays (~10 elements)
Works fine if the space is a letter eg: bw.write(n + "a")
Any idea what I'm doing wrong? Or is there an even easier way that I'm not seeing?
Thanks,
Civatrix
Can you explain how you're viewing/reading in the file afterwards? Your code is basically fine as far as I can see (bar moving the close() to a finally block), and it really should make no difference whatsoever whether a space or other letter is added. But that might make a difference e.g. to a text editor, I suppose...?
That code will write all the elements of a[] to the file followed by a space, unless it gets an exception. However it won't write any lines. Is that your problem? If so, you need bw.newLine() after each write().

How to create multiple files, generating a random name for each file

What I'm trying to do is create lots of random files but I'm just completely lost. The code below just doesn't work, I'm wondering why it doesn't work and what can i do to fix it.
import java.io.*;
import java.util.*;
public class main {
public static void main(String arg[]){
Random rn = new Random();
int randn = 1+rn.nextInt(999999990);
String I_S = Integer.toString(randn);
Formatter file;
try{
for(int i = 0; i < 9999999; i++){
file = new Formatter("%s.txt", I_S);
}
}
catch(Exception ex){
System.err.println("Error");
}
}
}
By just generating random numbers, you may end up creating / overwriting the same file multiple times. To have an absolutely unique set, do something like this:
final int MAX = 9999999;
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < MAX; i++) {
list.add(i);
}
Collections.shuffle(list);
for (int i = 0; i < MAX; i++) {
// create files here
}
How to create multiple files, generating a random name for each file.
You probably want to have a look at File.createTempFile.
Looking at your code, you probably want something like
Random rn = new Random();
String file;
for (int i = 0; i < 100; i++)
file = String.format("%i.txt", 1+rn.nextInt(999999990));
I'm not sure exactly what you expect this code to achieve, but right now it effectively does nothing.
You're looping many, many times creating an instance of Formatter each time. This is not a file; instead, it's a class that knows how to replace tokens in strings to create other strings.
I think you're confused by the description of the constructor you're calling and the purpose of the class as a whole. The constructor takes as its first argument the name of the file to use for output - in your case, this will always be %s.txt. The second argument is the name of a supported charset to use for encoding the String to the file.
This code will always fail because:
Your I_S variable, e.g., "56437890" is not a valid encoding (whereas "UTF-8" would be). Hence the constructor will probably throw an exception when trying to work out the encoding scheme.
Even if the charset was miraculously right, you're still trying to write to the same file (%s.txt) every time, so you wouldn't get the desired multi-file behaviour.
This string may not even be a valid filename, depending on your OS, and so if the Formatter tries to create the file it will throw an exception.
If both arguments miraculously work out, you're still not actually doing anything with the formatter, so it doesn't have anything to write out to the file which thus may not be created.
Finally, you're not updating your random variable (I_S) in the loop - it gets set once, and then maintains the same value forever. So even if all the above problems weren't issues, you'd still create the same (single) randomly-named file over and over and over again.
And as I noted in the comments, when it fails, you're catching and swallowing the exception so you have absolutely no way of knowing what went wrong.
Fundamentally I think you're confused about the purpose of the Formatter class, and since I don't know what you're trying to achieve (should the files be empty? Have specific text in?) I can't suggest something which definitely works. However, if you just want to create empty files, try something like this inside your loop:
String filename = "%s.txt".format(I_S);
File file = new File(filename);
file.createNewFile();
// Add some logic to update the random variable here!
As a final point, adarshr's answer is entirely right that you stand a nontrivial chance of repeating random numbers, so you won't get exactly as many files as you expect. The answer goes on to describe a good way to avoid this, and it's worth following.
You may want to look into writing something more like this:
void Write()
{
try {
for(int i = 0; i < MAX_FILES; i++) {
FileWriter outFile = new FileWriter("C:\File" + i.toString() + ".txt");
PrintWriter out = new PrintWriter(outFile);
// Write text to file
out.println("This is line 1");
out.println("This is line 2");
out.print("This is line3 part 1, ");
out.println("this is line 3 part 2");
out.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
// obviously this requires a import java.io.*; at the top of the class
Source: http://www.abbeyworkshop.com/howto/java/writeText/index.html

Categories