|
| ||||||||||
| Tags: class, host, java, object, program, string |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| How to write string data to file using java program?
I am new to this forum. I am B.Sc.I.T. graduate. Yesterday one of my friend asked me one question like How to write string data to file using java program? I unable to give answer. That's why I am asking this question on this forum. If you know solution of this question then give it to me. Thanks in advanced. Last edited by Linoo : 21-01-2010 at 07:55 PM. |
|
#2
| ||||
| ||||
| Re: How to write string data to file using java program?
I have written following code to write string data to file. Just go through it. Code: import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class WriteToFileExample
{
public static void main(String[] args)
{
try
{
File f = new File("java.txt");
String d = "started Learning Java Programming";
FileUtils.writeStringToFile(f, d);
} catch (IOException e)
{
e.printStackTrace();
}
}
} |
|
#3
| |||
| |||
| Re: How to write string data to file using java program?
Just go through following code. Code: import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
public static void main(String[] args) {
String p = new String("www.java2s.com\n");
File aF = new File("test.txt");
FileOutputStream outputFile = null;
try {
outputFile = new FileOutputStream(a, true);
System.out.println("File stream created successfully.");
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
}
FileChannel outChannel = outputFile.getChannel();
ByteBuffer b = ByteBuffer.allocate(1024);
System.out.println("New buffer: position = " + b.position()
+ "\tLimit = " + b.limit() + "\tcapacity = "
+ b.capacity());
for (char ch : p.toCharArray()) {
buf.putChar(ch);
}
System.out.println("Buffer after loading: position = " + b.position()
+ "\tLimit = " + b.limit() + "\tcapacity = "
+ b.capacity());
b.flip();
System.out.println("Buffer after flip: position = " + buf.position()
+ "\tLimit = " + b.limit() + "\tcapacity = "
+ b.capacity());
try {
outChannel.write(b);
outputFile.close();
System.out.println("Buffer contents written to file.");
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
} |
|
#4
| ||||
| ||||
| Re: How to write string data to file using java program?
You can easily write string data to file using following java program. Code: import java.io.*;
import java.util.*;
class WriteTest1
{
public static void main(String[] args)
{
try {
BufferedWriter out1 = new BufferedWriter(new FileWriter("java.txt"));
out.write("aString");
out1.newLine();
out1.write("this is a");
out1.newLine();
out1.write("ttest");
out1.close();
}
catch (IOException e)
{
System.out.println("Exception ");
}
return ;
} } |
|
#5
| ||||
| ||||
| Re: How to write string data to file using java program?
Hey you can use following code to do this process. Code: import java.io.*;
public class WriteTextFileEg{
public static void main(String[] args)throws IOException{
Writer op = null;
String textfile = "alfad khan";
File file = new File("writing.txt");
output = new BufferedWriter(new FileWriter(file));
op.write(textfile);
op.close();
System.out.println("Your file has been written");
}
} |
|
#6
| ||||
| ||||
| Re: How to write string data to file using java program?
You have to use following code to write string data to file. Just try to understand each line. Code: import java.io.*;
public class WriteFile{
public static void main(String[] args) throws IOException{
File f1 =new File("textfile1.txt");
FileOutputStream fop1=new FileOutputStream(f);
if(f1.exists()){
String strp="This data is written through the program";
fop1.write(strp.getBytes());
fop1.flush();
fop1.close();
System.out.println("following data has been written");
}
else
System.out.println("This file is not exist");
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "How to write string data to file using java program?" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java program to iterate a subset of a string. | Nadiaa | Software Development | 5 | 19-03-2012 01:53 PM |
| Need help to write this program in java? | frkadeel | Software Development | 1 | 01-12-2010 02:58 PM |
| How to compare string using java program regardless of their case? | MAGALY | Software Development | 4 | 30-01-2010 06:23 PM |
| program to reverse a string in java. | Deepest BLUE | Software Development | 3 | 26-11-2009 10:03 PM |
| how to write program on palindrome in java? | Linoo | Software Development | 3 | 26-11-2009 04:19 PM |