: , , . , , .. - (I/O), , , , . Java (stream) java.io. InputStream, OutputStream. Java , , .
File
File
java.io, . , . Java , , list.Java , UNIX DOS. UNIX '/', Windows Java '\'. , , DOS, , '\', , , , , \\java\\COPYRIGHT.
File . , File . , , . , :
import java.io.File;
class FileTest {
static void p(String s) {
System.out.println(s);
}
public static void main(String args[]) {
File f1 = new File("/java/COPYRIGHT");
p("File Name:" + f1 .getName());
p("Path:" + f1.getPath());
p("Abs Path:" + f1.getAbsolutePath());
p("Parent:" + f1.getParent());
p(f1.exists() ? "exists" : "does not exist");
p(f1.canWrite() ? "is writeable" : "is not writeable");
p(f1.canRead() ? "is readable" : "is not readable");
p("is " + (f1.isDirectory() ? " " : "not") + " a directory");
p(f1.isFile() ? "is normal file" : "might be a named pipe");
p(f1.isAbsolute() ? "is absolute" : "is not absolute");
p("File last modified:" + f1. lastModified());
p("File size:" + f1.length() + " Bytes");
} }
- :
File Name:COPYRIGHT ( )
Path:/java/COPYRIGHT ()
Abs Path:/Java/COPYRIGHT ( )
Parent:/java ( )
exists ( )
is writeable ( )
is readable ( )
is not a directory ( )
is normal file ( )
is absolute
File last modified:812465204000 ( )
File size:695 Bytes ( )
, ( ). renameTo(File dest) ( ). delete . , , , .
File, . File , isDirectory true. list . , list .
import java.io.File;
class DirList {
public static void main(String args[]) {
String dirname = "/java"; //
File f1 = new File(dirname);
if (f1.isDirectory()) { // f1
System.out.println("Directory of ' + dirname);
String s[] = f1.list();
for ( int i=0; i < s.length; i++) {
File f = new File(dirname + "/" + s[i]);
if (f.isDirectory()) { // f System.out.println(s[i] + " is a directory"):
} else {
System.out.println(s[i] + " is a file");
} } } else {
System.out.println(dirname + " is not a directory");
} }
}
/java :
:\> java DirList
Directory of /java
bin is a directory
COPYRIGHT is a file
README is a file
FilenameFilter
, list, , . java.io FilenameFilter. , , accept(), . accept true , , false , .
File , . mkdir . , , mkdirs , .
InputStream
InputStream , Java . IOException. InputStream.
OutputStream
InputStream, OutputStream . Java. void IOException . :
FilelnputStream
FileInputStream . , .
InputStream f0 = new FileInputStream("/autoexec.bat");
File f = new File("/autoexec.bat"):
InputStream f1 = new FileInputStream(f);
FileInputStream, . FileInputStream InputStream. mark reset IOException. , , . , available , , skip , .
import java.io.*;
import java.util.*;
class FileInputStreamS {
public static void main(String args[]) throws Exception {
int size;
InputStream f1 = new FileInputStream("/wwwroot/default.htm");
size = f1.available();
System.out.println("Total Available Bytes: " + size);
System.out.println("First 1/4 of the file: read()");
for (int i=0; i < size/4; i++) {
System.out.print((char) f1.read());
}
System.out.println("Total Still Available: " + f1.available());
System.out.println("Reading the next 1/8: read(b[])");
byte b[] = new byte[size/8];
if (f1.read(b) != b.length) {
System.err.println("Something bad happened");
}
String tmpstr = new String(b, 0, 0, b.length);
System.out.println(tmpstr);
System.out.println("Still Available: " + f1.available());
System.out.println("Skipping another 1/4: skip()");
f1.skip(size/4);
System.out.println( "Still Available: " + f1.available());
System.out.println("Reading 1/16 into the end of array");
if (f1.read(b, b.length-size/16, size/16) != size/16) {
System.err.println("Something bad happened");
}
System.out.println("Still Available: " + f1.available());
f1.close();
}
}
FileOutputStream
FileOutputStream , FileInputStream. , , . FileOutputStream , , .
, , System.in - , , 12- . . , file1.txt, , , , . , file2.txt, , . , , .
import java.io.*;
class FileOutputStreamS {
public static byte getlnput()[] throws Exception {
byte buffer[] = new byte[12];
for (int i=0; i<12; i++) {
buffer[i] = (byte) System.in.read();
}
return buffer;
}
public static void main(String args[]) throws Exception {
byte buf[] = getlnput();
OutputStream f0 = new FileOutputStream("file1.txt");
OutputStream f1 = new FileOutputStream("file2.txt");
OutputStream f2 = new FileOutputStream("file3.txt");
for (int i=0; i < 12; i += 2) {
f0.write(buf[i]);
}
f0.close();
f1.write(buf);
f1.close();
f2.write(buf, 12/4, 12/2);
f2.close();
} }
FileOutputStream . FileOutputStream, . - Java.
ByteArraylnputStream
ByteArrayInputStream - , byte. , . . .
String tmp = "abcdefghijklmnopqrstuvwxyz";
byte b[] = new byte [tmp.length()];
tmp. getBytes(0, tmp.length(), b, 0);
ByteArrayInputStream input1 = new ByteArrayInputStream(b);
ByteArrayInputStream input2 = new ByteArreyInputStream(b,0,3);
ByteArrayOutputStream
ByteArrayOutputStream . 32 . , ( 1024 ):
OutputStream out0 = new ByteArrayOutputStream();
OutputStream out1 = new ByteArrayOutputStream(1024);
ByteArrayOutputStream , , .
import java.io.*;
import java.util.*;
class ByteArrayOutputStreamS {
public static void main(String args[]) throws Exception {
int i;
ByteArrayOutputStream f0 = new ByteArrayOutputStream(12);
System.out.println("Enter 10 characters and a return");
while (f0.size() != 10) {
f0.write( System.in.read());
}
System.out.println("Buffer as a string");
System.out.println(f0.toString());
System.out.println ("Into array");
byte b[] = f0.toByteArray();
for (i=0; i < b.length; i++) {
System.out.print((char) b[i]);
}
System.out.println();
System.out. println("To an OutputStream()");
OutputStream f2 = new File0utput8tream("test.txt");
f0.writeTo(f2);
System.out.println("Doing a reset");
f0. reset();
System.out.println("Enter 10 characters and a return");
while (f0.size() != 10) {
f0.write (System.in.read());
}
System.out.println("Done.");
} }
test.txt, , :
:\> type test.txt
0123456789
StringBufferlnputStream
StringBufferInputStream ByteArrayInputStream , String, . , Java StringBufferedOutputStream. :
StringBufferInputStream( String s)
. . , , , InputStream OutputStream, , .
, - . :
BufferedInputStream
- . BufferedlnputStream Java InputStream , , , . .
BufferedInputStream(InputStream in)
, 32 .
BufferedInputStream(InputStream in, int size)
. , .
BufferedOutputStream
BufferedOutputStream OutputStream , flush, . :
BufferedOutputStream(OutputStream out)
32 . :
BufferedOutputStream(OutputStream out, int size)
.
PushbacklnputStream
pushback ( ). Pushback InputStream , . PushbacklnputStream - IOException.
PushbackInputStream(InputStream in)
InputStream, PushbacklnputStream unread(int ch), ch .
SequencelnputStream
SequencelnputStream . SequenceInputStream InputStream, , InputStream:
SequenceInputStream(Enumeration e) SequenceInputStream(InputStream s0, InputStream s1)
, , , ..
PrintStream , System . System.out.println, , . PrintStream : PrintStream(OutputStream out) PrintStream(OutputStream out, boolean autoflush). autoflush , Java .
Java- PrintStream print println, , Object. , toString Object, .
Java , , int float. C++ , , , , - .
Java - . Java-, - InputStream OutputStream, - , -. , , , .
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |