**********************code**************************
package v1;
import java.io.*;
public class CreateBackup {
private String textContent;
public CreateBackup(String fromFile) {
try {
File input = new File(fromFile);
BufferedReader br = new BufferedReader(new FileReader(input));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line + "\n");
}
br.close();
textContent = sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void copy(String toFile) {
try {
File output = new File(toFile);
PrintWriter out = new PrintWriter(new BufferedWriter(new
FileWriter(output)));
out.println(textContent);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
*******this is how I call CreateBackup in main class*******************
private void openFileDialog() {
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setText(getResourceString("dialog.File.Open.text"));
dialog.setFilterPath(System.getProperty("user.dir"));
String[] filterExt = {"*.txt", "*.rtf", "*.doc", "*.*"};
dialog.setFilterExtensions(filterExt);
String fileName = dialog.open();
// TODO Create backup file first before open the file
String outFile = fileName + extension;
CreateBackup backup = new CreateBackup(fileName);
backup.copy(outFile);
// Open the file
try {
File selectedFile = new File(fileName);
if (selectedFile.isFile()) {
try {
BufferedReader br = new BufferedReader(new FileReader(selectedFile));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
text.setText(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
Use a FileInputStream, and a FileOutputStream, and copy a small amount
at a time (say 8k) using a byte array.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
>Hi,
>I'm trying to create a class which only jobs is to copy any text files
You might use my FileTransfer class to copy files for you. See
http://mindprod.com/jgloss/products.html#FILETRANSFER
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
>"main" java.lang.OutOfMemoryError: Java heap space
see http://mindprod.com/jgloss/runerrormessages.html#OUTOFMEMORYERROR
Thanks its working now, using a method to create backup instead of
another class. Anyway, while working with FileOutputStream I've noticed
that if Im doing this then it just run,
FileInputStream from = new FileInputStream(fromFile);
FileOutputStream to = new FileOutputStream(toFile);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesWrite;
while ((bytesWrite = from.read(buffer)) != -1) {
to.write(buffer, 0, bytesWrite);
}
but if I'm assigning "from.read(buffer)" to byteWrite first, then the
apps freeze. Why can't I do this?
FileInputStream from = new FileInputStream(fromFile);
FileOutputStream to = new FileOutputStream(toFile);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesWrite = from.read(buffer);
while (bytesWrite != -1) {
to.write(buffer, 0, bytesWrite);
}
while (me.browsing()) {
throw new FileNotFoundException("Page doesn't exist");
}
Thanks but I already fix it using classes Daniel told me.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
...
// Get an input channel
final FileChannel ich = new FileInputStream(input).getChannel();
// Get an output channel
final FileChannel och = new FileOutputStream(tofile).getChannel();
// Get the size of the input
final long size = ich.size();
// Begin at the beginning
long position = 0;
// Copy chunks of the file until finished
while (position < size)
position += och.transferFrom(ich, position, size - position);
// Close the files
ich.close();
och.close();
--
John W. Kennedy
"Sweet, was Christ crucified to create this chat?"
-- Charles Williams. "Judgement at Chelmsford"
>http://mindprod.com/jgloss/products.html#FILETRANSFER
oops
http://mindprod.com/products.html#FILETRANSFER
public static void makeCopy(File from, File to) throws IOException
{
if(!to.exists())
{
to.createNewFile();
}
InputStream in = new FileInputStream(from);
OutputStream out = new BufferedOutputStream(new
FileOutputStream(to));
int b;
while((b = in.read()) != -1)
{
out.write(b);
}
out.flush();
in.close();
out.close();
}
wunder wrehe i puttit.