How to write string data to file using java program?
Hello to all,
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.
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();
}
}
}
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);
}
}
}
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 ;
} }
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");
}
}
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");
}
}