2.FileIO
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");//Die zu schreibende Datei wird hier angegeben.
out.write(data);//Ich schreibe hier Charaktere.
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
}
import java.io.*;
public class SampleOutputStream {
private static final String NEW_LINE = "\n";//Konstante des Zeilenvorschubcodes
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);//Die erste Zeile
out.write(NEW_LINE.getBytes());//Neue Zeile
out.write(data);//2. Zeile
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
//Holen Sie sich den absoluten Pfad der Datei
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;
//Ich lese eine Datei.
//read()Wenn die Methode zu Ende geht-Weil es 1 zurückgibt.-Drehen Sie die Schleife, bis 1 zurückgegeben wird.
while ((i = in.read()) != -1) {
char character = (char) i;
System.out.print(character);//Ausgabe an die Konsole
}
} 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-Einstellungen
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/");//Geben Sie eine Webseite an.
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");//Geben Sie die Quelldatei an
out = new FileOutputStream("OutputText.txt");//Geben Sie die Kopierzieldatei an
inChannel = in.getChannel();
outChannel = out.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);//Datei kopieren
} 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");//Der Zeichencode wird hier angegeben.
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