Dæmi um Java skrifa til skráar

Í þessari færslu munum við skoða fimm mismunandi dæmi um hvernig á að skrifa í skrá með Java. Kóðinn tappar til að athuga hvort skráin sé til áður en hún er skrifuð í skrána, annars er skrá búin til.

Skrifaðu í skrána með BufferedWriter

import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class WriteToFile {
public static void main( String[] args ) {
try {

String content = 'Content to write to file';

//Name and path of the file

File file = new File('writefile.txt');

if(!file.exists()){


file.createNewFile();

}

FileWriter fw = new FileWriter(file);

BufferedWriter bw = new BufferedWriter(fw);

bw.write(content);

bw.close();

} catch(IOException ex) {

System.out.println('Exception occurred:');

ex.printStackTrace();
}
} }
Athugið:Ef við viljum bæta við skrá, verðum við að frumstilla FileWriter með satt breytu: FileWriter fw = new FileWriter(file, true);

Tengt:

Skrifaðu í skrána með PrintWriter

import java.io.*; public class WriteToFile {
public static void main( String[] args ) {
try {

String content = 'Content to write to file';

//Name and path of the file

File file = new File('writefile.txt');

if(!file.exists()){


file.createNewFile();

}

FileWriter fw = new FileWriter(file);

PrintWriter bw = new PrintWriter(fw);

bw.write(content);

bw.close();

} catch(IOException ex) {

System.out.println('Exception occurred:');

ex.printStackTrace();
}
} }

Skrifaðu í skrána með FileOutputStream

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class WriteToFile {
public static void main( String[] args ) {
try {

String content = 'Content to write to file';

//Name and path of the file

File file = new File('writefile.txt');

if(!file.exists()){


file.createNewFile();

}

FileOutputStream outStream = new FileOutputStream(file);

byte[] strToBytes = content.getBytes();

outStream.write(strToBytes);

outStream.close();

} catch(IOException ex) {

System.out.println('Exception occurred:');

ex.printStackTrace();
}
} }

Skrifaðu í skrána með því að nota Files flokk

import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class WriteToFile {
public static void main( String[] args ) {
Path path = Paths.get('writefile.txt');
String content = 'Content to write to file';

try {

byte[] bytes = content.getBytes();

Files.write(path, bytes);
} catch(IOException ex) {

System.out.println('Exception occurred:');

ex.printStackTrace();
}
} }

Skrifaðu í skrána með því að nota DataOutputStream

import java.io.*; public class WriteToFile {
public static void main( String[] args ) {
String content = 'Content to write to file';

try {

File file = new File('writefile.txt');

if(!file.exists()){


file.createNewFile();

}

FileOutputStream fos = new FileOutputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(fos);

DataOutputStream dataOutStream = new DataOutputStream(bos);

dataOutStream.writeUTF(content);

dataOutStream.close();

} catch(IOException ex) {

System.out.println('Exception occurred:');

ex.printStackTrace();
}
} }