| 1 |
14 |
jlechner |
/*************************************************************************
|
| 2 |
|
|
/* FileWriterTest.java -- Test of FileWriter and OutputStreamWriter classes
|
| 3 |
|
|
/*
|
| 4 |
|
|
/* Copyright (c) 1998 Free Software Foundation, Inc.
|
| 5 |
|
|
/* Written by Aaron M. Renn (arenn@urbanophile.com)
|
| 6 |
|
|
/*
|
| 7 |
|
|
/* This program is free software; you can redistribute it and/or modify
|
| 8 |
|
|
/* it under the terms of the GNU General Public License as published
|
| 9 |
|
|
/* by the Free Software Foundation, either version 2 of the License, or
|
| 10 |
|
|
/* (at your option) any later version.
|
| 11 |
|
|
/*
|
| 12 |
|
|
/* This program is distributed in the hope that it will be useful, but
|
| 13 |
|
|
/* WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 14 |
|
|
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 15 |
|
|
/* GNU General Public License for more details.
|
| 16 |
|
|
/*
|
| 17 |
|
|
/* You should have received a copy of the GNU General Public License
|
| 18 |
|
|
/* along with this program; if not, write to the Free Software Foundation
|
| 19 |
|
|
/* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
| 20 |
|
|
/*************************************************************************/
|
| 21 |
|
|
|
| 22 |
|
|
import java.io.*;
|
| 23 |
|
|
|
| 24 |
|
|
public class FileWriterTest
|
| 25 |
|
|
{
|
| 26 |
|
|
|
| 27 |
|
|
public static void
|
| 28 |
|
|
main(String[] argv)
|
| 29 |
|
|
{
|
| 30 |
|
|
System.out.println("Starting test of FileWriter and OutputStreamWriter");
|
| 31 |
|
|
|
| 32 |
|
|
System.out.println("Test 1: File Write Test");
|
| 33 |
|
|
try
|
| 34 |
|
|
{
|
| 35 |
|
|
String s1 = "Ok, what are some of the great flame wars that we have " +
|
| 36 |
|
|
"had lately. Let us see, there was emacs vs. xemacs, " +
|
| 37 |
|
|
"KDE vs. Gnome, and Tcl vs. Guile";
|
| 38 |
|
|
|
| 39 |
|
|
String s2 = "Operating systems I have known include: solaris, sco, " +
|
| 40 |
|
|
"hp-ux, linux, freebsd, winblows, os400, mvs, tpf, its, multics";
|
| 41 |
|
|
|
| 42 |
|
|
//File f = File.createTempFile("fostest", new File("/tmp"));
|
| 43 |
|
|
File f = new File("/tmp/000001");
|
| 44 |
|
|
FileWriter fw = new FileWriter(f.getPath());
|
| 45 |
|
|
|
| 46 |
|
|
char buf[] = new char[s1.length()];
|
| 47 |
|
|
s1.getChars(0, s1.length(), buf, 0);
|
| 48 |
|
|
fw.write(buf, 0, 32);
|
| 49 |
|
|
fw.write(buf, 32, s1.getBytes().length - 32);
|
| 50 |
|
|
fw.close();
|
| 51 |
|
|
|
| 52 |
|
|
fw = new FileWriter(f.getPath(), true);
|
| 53 |
|
|
buf = new char[s2.length()];
|
| 54 |
|
|
s2.getChars(0, s2.length(), buf, 0);
|
| 55 |
|
|
fw.write(buf);
|
| 56 |
|
|
fw.close();
|
| 57 |
|
|
|
| 58 |
|
|
if (f.length() != (s1.length() + s2.length()))
|
| 59 |
|
|
throw new IOException("Incorrect number of chars written");
|
| 60 |
|
|
|
| 61 |
|
|
f.delete();
|
| 62 |
|
|
|
| 63 |
|
|
System.out.println("PASSED: File Write Test");
|
| 64 |
|
|
}
|
| 65 |
|
|
catch(IOException e)
|
| 66 |
|
|
{
|
| 67 |
|
|
System.out.println("FAILED: File Write Test: " + e);
|
| 68 |
|
|
}
|
| 69 |
|
|
|
| 70 |
|
|
System.out.println("Test 2: Permission Denied Test");
|
| 71 |
|
|
try
|
| 72 |
|
|
{
|
| 73 |
|
|
FileWriter fw = new FileWriter("/etc/newtempfile");
|
| 74 |
|
|
System.out.println("FAILED: Permission Denied Test");
|
| 75 |
|
|
}
|
| 76 |
|
|
catch (IOException e)
|
| 77 |
|
|
{
|
| 78 |
|
|
System.out.println("PASSED: Permission Denied Test: " + e);
|
| 79 |
|
|
}
|
| 80 |
|
|
|
| 81 |
|
|
System.out.println("Finished test of FileWriter");
|
| 82 |
|
|
}
|
| 83 |
|
|
|
| 84 |
|
|
} // class FileWriter
|
| 85 |
|
|
|