编写一个 java 程序来读取一个冗长的字符串(使用流)并将输出写入一个文件?
Write a java program to read from a lengthy string (using streams) and write the output to a file?
如何使用流获取冗长的字符串作为输入以及如何将输出写入文件?
/*
Here is another way you can perform the storage.
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteExample {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "This is the text content";//our you can take it
try {
file = new File("c:/newfile.txt");//File creation at C:/
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
finally{
if(fop!=null){
fop.flush();
fop.close();
}
}
这是一个示例,说明如何读取字符串并将其写入输出文件,以及如何读取输入文件并将其写入输出文件。
还提到了字符流到字节流的转换,反之亦然
package task;
import java.io.*;
//char -> byte (input - byte , output - char);
//byte -> char (input - char , output - byte);
// 1.You have to use OutputStreamWriter class for converting Character stream to Byte stream.
// 2.InputStreamReader class for converting Byte stream to Character stream,
// as these classes are used for stream conversions between two different streams.
public class Ques1{
public static void main(String args[]) throws IOException{
//byte stream
FileInputStream in = new FileInputStream("input.txt");
//FileOutputStream out = new FileOutputStream("output.txt");
//char stream
//FileReader in = new FileReader("input.txt");
FileWriter out = new FileWriter("output.txt");
try{
String s = "answet";
int i = 0;
while(i<s.length()){
out.write(s.charAt(i));
i++;
}
int t;
// while((t=(in.read()))!=-1){
// out.write(t);
// }
}
finally{
if(in!=null) in.close();
if(out!=null) out.close();
}
}
}
如何使用流获取冗长的字符串作为输入以及如何将输出写入文件?
/*
Here is another way you can perform the storage.
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteExample {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "This is the text content";//our you can take it
try {
file = new File("c:/newfile.txt");//File creation at C:/
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
finally{
if(fop!=null){
fop.flush();
fop.close();
}
}
这是一个示例,说明如何读取字符串并将其写入输出文件,以及如何读取输入文件并将其写入输出文件。
还提到了字符流到字节流的转换,反之亦然
package task;
import java.io.*;
//char -> byte (input - byte , output - char);
//byte -> char (input - char , output - byte);
// 1.You have to use OutputStreamWriter class for converting Character stream to Byte stream.
// 2.InputStreamReader class for converting Byte stream to Character stream,
// as these classes are used for stream conversions between two different streams.
public class Ques1{
public static void main(String args[]) throws IOException{
//byte stream
FileInputStream in = new FileInputStream("input.txt");
//FileOutputStream out = new FileOutputStream("output.txt");
//char stream
//FileReader in = new FileReader("input.txt");
FileWriter out = new FileWriter("output.txt");
try{
String s = "answet";
int i = 0;
while(i<s.length()){
out.write(s.charAt(i));
i++;
}
int t;
// while((t=(in.read()))!=-1){
// out.write(t);
// }
}
finally{
if(in!=null) in.close();
if(out!=null) out.close();
}
}
}