2.FileIO --Write to text file --Insert line breaks in text files --Reading a text file --Loading web page content --Copy file to another file --Specify the character code and read the text file (measures against garbled characters) --Write a string to a text file --Read a text file string
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SampleOutputStream {
public static void main(String[] args) throws IOException {
String text = "Hello World.";
byte[] data = text.getBytes();
OutputStream out = null;
try {
out = new FileOutputStream("OutputText.txt");//The file to be written is specified here.
out.write(data);//I am writing characters here.
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
}
import java.io.*;
public class SampleOutputStream {
private static final String NEW_LINE = "\n";//Line feed code constant
public static void main(String[] args) throws IOException {
String text = "Hello World.";
byte[] data = text.getBytes();
OutputStream out = null;
try {
out = new FileOutputStream("OutputText.txt");
out.write(data);//The first line
out.write(NEW_LINE.getBytes());//new line
out.write(data);//2nd line
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
//Get the absolute path of the file
File file = new File("OutputText.txt");
String str = file.getAbsolutePath();
System.out.println("pass : " + str);
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class SampleInputStream {
public static void main(String[] args) throws IOException {
InputStream in = null;
try {
in = new FileInputStream("InputText.txt");
int i = -1;
//I'm reading a file.
//read()When the method goes to the end-Because it returns 1.-Turn the loop until 1 is returned.
while ((i = in.read()) != -1) {
char character = (char) i;
System.out.print(character);//Output to console
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
}
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class SampleInputStream {
public static void main(String[] args) throws IOException {
//proxy settings
System.setProperty("http.proxyHost","test.proxy.com");
System.setProperty("http.proxyPort","8081");
System.setProperty("https.proxyHost","test.proxy..com");
System.setProperty("https.proxyPort","8081");
System.setProperty("http.nonProxyHosts","localhost|*.nonproxy.com");
URL url = new URL("https://www.google.co.jp/");//Specify a web page.
InputStream in = null;
try {
in = url.openStream();
int i = -1;
while ((i = in.read()) != -1) {
char character = (char) i;
System.out.print(character);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class SampleOutputStream {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
in = new FileInputStream("InputText.txt");//Specify the copy source file
out = new FileOutputStream("OutputText.txt");//Specify the copy destination file
inChannel = in.getChannel();
outChannel = out.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);//Copy file
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (outChannel != null) {
outChannel.close();
}
if (inChannel != null) {
inChannel.close();
}
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
}
}
}
import java.io.*;
public class SampleInputStream {
public static void main(String[] args) throws IOException {
InputStream in = null;
InputStreamReader inr = null;
try {
in = new FileInputStream("InputText.txt");
inr = new InputStreamReader(in, "UTF-8");//The character code is specified here.
int i = -1;
while ((i = inr.read()) != -1) {
char character = (char) i;
System.out.print(character);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (inr != null) {
inr.close();
}
if (in != null) {
in.close();
}
}
}
}
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class SampleOutputStream {
public static void main(String[] args) throws IOException {
FileWriter out = null;
try {
out = new FileWriter("OutputText.txt");
out.write("Hello World.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class SampleBufferedReader {
public static void main(String[] args) throws IOException {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("InputText.txt");
br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (br != null) {
br.close();
}
if (fr != null) {
fr.close();
}
}
}
}
Recommended Posts