Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write strings into text file

Status
Not open for further replies.

bbvic

Technical User
Oct 21, 2004
51
US
this codes works with 1.5 , but it does not work with 1.4..
The error sign is that the method format(String, Object[]) is undefined for the type String.
can you help me how it works with 1.5?

import java.io.*;
import java.util.*;


public class Main {
public static void main(String[] args) {

String [] test =null;
String str1="Today";
String str2="is";
String str3="monday";
String str4="good";

String format = "%1$-9s%2$-3s";
format +="%3$-30s%4$-30s\n";
test = new String[] {str1, str2, str3,str4};
File f = new File("D:\\temp\\test.txt");

try
{
if ( f.exists() )
{

PrintStream ps = new PrintStream(new FileOutputStream("D:\\temp\\test.txt"));
ps.println(String.format(format,(Object[])test));
ps.flush();
ps.close();
}
else
{
PrintStream ps = new PrintStream(new FileOutputStream("D:\\temp\\test.txt"));
ps.println(String.format(format,(Object[])test));
ps.flush();
ps.close();
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
 
Hi,
Take a look at the apache commons
I use the "org.apache.commons.io.FileUtils"
It's very easy.
-----------
String2File
-----------
FileUtils.writeStringToFile("filename.txt","my content","UTF-8");

-----------
File2String
-----------
String content = FileUtils.readFileToString("filename.txt","UTF-8"));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top