I want to save my ArrayList in a txt file, so i used PrintWriter. It is like a table, so I used the String.format method. When writing my data on the console, then it is perfect. But when writing on txt file it was different.
Why is it different when writing in txt file from console. Just see in below image?
It's probably because of tabs (\t) you are using. Apparently notepad has a different way of displaying tabs than the console. You might be able to use String.format to do some formatting, instead of adding tabs yourself, just check the JavaDoc for that.
See this question for a solution, for example:
String.format("%s, %s, %-20s%n", firstName, lastName, phoneNumber)
EDIT
Additional tip: you can add %n in the format string to get a platform specific newline (Added in above example).
Related
I am making a java program where I input answers for a friendship survey. It spits out the student's top ten friends. However I need to print out the results and give them to the students. The old of doing it was to have the java program write to write html then we would open each file one at a time and print out the page. However, having 400+ students to do it for takes a while.
So since I am re making the program I would like to make it so I can just have it on word files and print them all out at once. However, I don't know how to write to a word file and notepad isn't stylish enough. Anyone know how to make this possible or another way that is easier?
I did a similar thing some years ago, using Rich Text Format. Its advantage is that it's a plain text format that can easily be manipulated.
I created the form document in Word with some unique placeholder strings where I'd later fill in the actual data and saved it as RTF.
With a text editor, I made sure that Word didn't split the placeholders by inserting some junk formatting directives, and corrected that manually where necessary.
Then, filling in the actual data just meant to do some simple text replacement (in my case, there was no risk to interfere with the formatting directives), and saving the resulting RTF file.
As Word typically opens RTF files just as easy as DOC or DOCX ones, this was an easy working solution for me.
I've done a lot of internet searching to find some information to no avail.. Hopefully you can help me..
I want to be able to use a flat file, with normal content (i.e. full english sentences, paragraphs etc), extract each word and store each word individually, one word per row, in a SQL database (doesn't matter if there are spaces but characters such as apostrophes can be kept in)
I then want to have a HTML page with code to access this DB and output the text to the user one word at a time, essentially 'writing' the inputted files text word-by-word on the web page.
This is just a coding exercise but I am frustrated as I know the what but not the how.. I am not sure where to start. Please note some of these files can be quite big ~ 20,000 words so there may be a performance element to consider to any solution.
TL;DR: I want to extract individual words from a text file with normal everyday sentences into a SQL DB that I can retrieve from a HTML page.
Simple read & split exercise
with open(<filename>) as f:
dd = {}
for ln in f:
wds = ln.strip().split()
for word in wds:
dd[word] = 1 # need something for value
for wkey in dd:
<insert into db>
Well, before you start you should choose just one programming language. Since you seem like you are a beginner I would highly recommend Python over Java, but it depends on if you're required to use any particular language by an employer/professor/etc.
Also just to point out, this is also a very BIG task that you've chosen. I'll try to break it down into parts for you, but I recommend starting with just one of these parts before you move on, and make sure it works on your local machine before you try putting it on the web.
First you need to use something read in your file, preferably line by line. A method similar to FileReader/BufferedReader in Java or the open(), readlines() functions in Python will do these. I would also check out the tutorials online on file handling for whichever of these two languages you're going to use. The Python one is here. Practice this with a test file or a small section of your real file before you start working on your real input files.
When you start processing the lines from the file, I would recommend splitting them into individual words using a string split function on spaces or on any punctuation, such as ,.!?". This way you'll pull out the individual words from the each line in the file.
Next, you'll want to choose a database API for the appropriate programming language. I used PyMySQL but there is also MySQLDB for Python. In Java there is JDBC.
You'll need to then build your database on a server somewhere, preferably on the same server as your HTML page for ease of connection. You'll want to practice connecting to your database and adding sample rows before you start trying to process your real input files.
You can't have normal HTML access the database directly - you'll need to use a coding language like Python for that. I've never used Java for webpages, but with Python you'll simply output text and tell the server to display it as the webpage. This will do the trick:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import otherstuffhere
## Must have this header to tell browser how to handle this output
## and must be printed first
print ("Content-Type: text/html\n\n")
## Connect to database here
## Your code to display words from the database goes below here
print (myfield1)
Also remember that when you output your text, you'll need to add all the HTML tags to the normal text output. For example, when printing each word, you'll need to add <p> or <br> to end each line, because although the Python print() function will automatically add a line break, this doesn't translate to a line break in HTML. For example:
print ("My word list is: <br>")
for word in dbOutputList:
print (word)
print ("<br>")
After that the REAL fun/crying begins, but you should work on the above before you move on.
I see this to make text file and it also helps me out but in all examples i see that they just making string in notepad or we can say text file...
Can any one say that how to make table formatted text file in android??
i want to make file(invoice)
This is most likely going to involve some some slightly messy string processing. Assuming you have your data in an acceptable format (such as string arrays), you should be able to construct a single java string representing the whole table, and then use the code you found already to print it to a file. Use the escape character \t to separate between columns and \n to separate between rows.
That would be TSV format, and it is very easy to generate. Just add a TAB after every field, and a CR/LF pair after every record.
I have a method that writes a string to a text file using a DataOutputStream and the .writeBytes(String) method. If I write a string with a newline character, for example "I need \n help!", the new line is not displayed in notepad or other basic text editors. However, it does show up in WordPad, MS Word, etc. Why is this and can I fix it?
Mostly by using real text editors, which Notepad isn't.
You need to write system-specific newlines if you're not going to use a text editor that understands different flavors, or filter the file through something that does the conversion for you.
System.getProperty("line.separator");
This will give you an OS-specific line separator. It's less useful than you think.
System.out.printf("%n");
This does the same (and is available in String.format as well); also less useful than you think. It's more an editor thing, since any file could exist on any system, edited with any editor.
You should use System.getProperty("line.separator"); instead of directly using \n.
I'm developing a text editor in J2ME for editing source code, and because it has special features like syntax coloring, I can't use the regular TextBox, so I have to make a text box from scratch, using Canvas.
I found the way of reading/writing files from/to memory card, using FileConnection and the InputStreamReader/OutputStreamWriter classes for read and write text.
Now the problem is, when I read the file, how I can store the read information in memory, in order to edit the text freely and decide later if I can save or discard the changes?
Do I create a temporary file where I store the data for editing? But how can I write/delete text in middle of the file? Or do I have to dump the data in a StringBuffer?
Any methods or alternatives will be welcome.
Thanks!
I'd just use String (for storing the whole text in one variable)
or Vector of Strings (for storing the text line by line).
Temporary files is a very bad solution.