Java by API/java.io/FileWriter — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Содержание
FileWriter: close()
import java.io.File;
import java.io.FileWriter;
public class Main {
public static void main(String[] args) throws Exception{
File file = new File("user.txt");
FileWriter writer = new FileWriter(file, true);
writer.write("username=java;password=secret" + System.getProperty("line.separator"));
writer.flush();
writer.close();
}
}
FileWriter: flush()
import java.io.File;
import java.io.FileWriter;
public class Main {
public static void main(String[] args) throws Exception{
File file = new File("user.txt");
FileWriter writer = new FileWriter(file, true);
writer.write("username=java;password=secret" + System.getProperty("line.separator"));
writer.flush();
writer.close();
}
}
FileWriter: write(int i)
/*
* Output:
*
*/
import java.io.FileReader;
import java.io.FileWriter;
//copy file
public class MainClass {
public static void main(String args[]) {
try {
FileReader fr = new FileReader(args[0]);
FileWriter fw = new FileWriter(args[1]);
int i;
while ((i = fr.read()) != -1) {
fw.write(i);
}
fw.close();
fr.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
new FileWriter(File file)
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File inputFile = new File("farrago.txt");
File outputFile = new File("outagain.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
}
new FileWriter("LPT1:")
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class MainClass {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("LPT1:");
PrintWriter pw = new PrintWriter(fw);
String s = "www.jexp.ru";
int i, len = s.length();
for (i = 0; len > 80; i += 80) {
pw.print(s.substring(i, i + 80));
pw.print("\r\n");
len -= 80;
}
if (len > 0) {
pw.print(s.substring(i));
pw.print("\r\n");
}
pw.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
new FileWriter(String fileName, boolean append)
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
public static void main(String[] argv) throws Exception {
BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
out.write("aString");
out.close();
}
}
new FileWriter(String string)
/*
* Output:
*
*/
import java.io.FileReader;
import java.io.FileWriter;
//copy file
public class MainClass {
public static void main(String args[]) {
try {
FileReader fr = new FileReader(args[0]);
FileWriter fw = new FileWriter(args[1]);
int i;
while ((i = fr.read()) != -1) {
fw.write(i);
}
fw.close();
fr.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}