.


:




:

































 

 

 

 


InputStreamReader OutputStreamWriter

InputStream.. 3

OutputStream.. 4

/.. 5

. 5

File. 6

RandomAccessFile. 9

StreamTokenizer. 10

InputStreamReader OutputStreamWriter. 13

. 14

. 17

 


/ Java java.io. , , . : FileInputStream, FileOutputStream.

InputStream

InputStream . InputStream java.io , ( ) IOException:

public InputStream()

InputStream .

public abstract int read() throws IOException

, 0 255 ( 128 127). 1. .

public int read(byte[] buf)

. , buf , buf.length. 1 .

public int read(byte[] buf, int off, int len)

. , buf, off, len , buf.

public long skip(long count)

count . count - . .

public int available()

, .

public void close()

. (, ), . , , finalize .

OutputStream

OutputStream InputStream; , . , ( ) :

public OutputStream()

OutputStream .

public abstract void write(int b)

b. int, . , byte, int, int byte. , , 8 int 24 . .

public void write(byte[] buf)

. .

public void write(byte[] buf, int offset, int len)

, buf [offset] count , .

public void flush() throws IOException

, , .

public void close() throws IOException

. , .


/

/ Java FileInputStream FileOutputStream. :

String, .

File.

FileDescriptor.

FileDescriptor - , . getFD File RandomAccessFile. FileDescriptor File RandomAccessFile , , . , . , , , FileDescriptor.

flush FileOutputStream . , .

BufferedInputStream BufferedOutputStream , / . , .

, . byte , .

read BufferedInputStream, : read -, .

BufferedOutputStream. write , write -, .

, , :

OutputStream bufferedFile(String path)

throws IOExceptioon

{

OutputStream out = new FileOutputStream(path);

return new BufferedOutputStream(out);

}

FileOutputStream, BufferedOutputStream -. , .

FileOutputStream, , , - . , , .

File

File . , , .

File , . , , , File , exists.

. : ( ), . , char separatorChar String separator. .

File :

1. public File(String path)

File path. null, NullPointerException.

2. public File(String dirName, String name)

File name, dirName. dirName null, name. :

File(dirname + File.separator + name)

3. public File(File fileDir, String name)

File - fileDir File name. :

File(fileDir.getPath(), name)

get File. :

File src = new File("ok", "FileMethods");

System.out.println("getName() = " + src.getName());

System.out.println("getPath() = " + src.getPath());

System.out.println("getAbsolutePath() = "

+ src.getAbsolutePath());

System.out.println("getParent() = " + src.getParent());

 

:

getName() = FileMethods

getPath() = ok/FileMethods

getAbsolutePath() = /vob/java_prog/src/ok/FileMethods

getParent() = ok

, , File:

exists: true, .

canRead: true, .

canWrite: true, .

isFile: true, ( ).

isDirectory: true, .

isAbsolute: true, .

File :

public long lastModified()

. , , , . .

public long length()

.

public boolean mkdir()

true .


 

public boolean mkdirs()

, , true . , , , .

public boolean renameTo(File new_name)

true .

public boolean delete()

, File, true .

public String[] list()

. File , , null; . , . .. ( ).

public String[] list(FilenameFilter filter)

(. Filename Filter).

File.equals . File , , , . File.equals , File .

FileOutputStream RandomAccessFile, File.

, , File.pathSeparatorChar File.pathSeparator , . , UNIX : .:/bin:/usr/bin. , UNIX pathSepar a torChar .

String. File .


RandomAccessFile

RandomAccessFile , . Input Stream OutputStream, / . . Random AccessFile Data InputStream DataOutput Stream, / Java.

RandomAccessFile , read write. , , RandomAccessFile , InputStream OutputStream. , RandomAccessFile - DataInput DataOutput.

RandomAccessFile :

public RandomAccessFile(String name, String mode)

RandomAccessFile . r rw / . IOException.

public RandomAccessFile(File file, String mode)

RandomAccessFile File .

public RandomAccessFile(FileDescriptor fd)

RandomAccessFile fd File Descriptor (. FileDescriptor).

(random access), , / . :

public long getFilePointer()

( ) .

public void seek(long pos)

( ). pos.

public long length() throws IOException

.

StreamTokenizer

, java.io StreamTokenizer . 8 Unicode, Latin-1, , , 256 . , \u00ff, . (, ), , , ? (\u270D). .

, StreamTokenizer InputStream . nextToken, . , StreamTokenizer.

nextToken , ttype. :

TT_WORD: . sval String.

TT_NUMBER: . nval double. ( ). 3.4e79 , 0xffff .

TT_EOL: .

TT_EOF: .

. , , , , , . . , . , ‘ , ( ttype) ‘ int.

, :

public void wordChars(int low, int hi)

; TT_WORD. . , .


 

public void whitespaceChars(int low, int hi)

. ; , . wordChars, , -.

public void ordinaryChar (int ch)

ch . , .

public void ordinaryChars (int low, int hi)

.

public void commentChar (int ch)

ch ch .

public void quoteChar (int ch)

ch . , ch , sval ( -). Java \ (, \t), . , StreamTokenizer, Java. \xxxx, \, \" ( ) \Q, Q - ch. -, . , , -, ; -, .

public void parseNumbers()

. StreamTokenizer TT_NUMBER, nval. ordinaryChars , ( ), resetSyntax.

public void resetSyntax()

, . resetSyntax , nextToken , InputStream.read.

. StreamTokenizer:

1. wordChars(a, z);

2. wordChars(A, Z);

3. wordChars(128 + 32, 255);

4. whitespaceChars(0, );

5. commentChar(/);

6. quoteChar(");

7. quoteChar(\);

8. parseNumbers();

:

public void eolIsSignificant(boolean flag)

flag true, , nextToken TT_EOL. - TT_EOL . false.

public void slashStarComments(boolean flag)

flag true, /*...*/. false.

public void slashSlashComments(boolean flag)

flag true, // . false.

public void lowerCaseMode(boolean flag)

flag true, TT_WORD , ( String.toLowerCase). false.

:

public void pushBack()

. nextToken . ; pushBack .

public int lineno()

. .

public String toString()

, .

InputStreamReader OutputStreamWriter

java.io , Unicode -Unicode . InputStreamReader . OutputStreamWriter.

InputStreamReader OutputStreamWriter, , . , UTF-8 Unicode, :

FileInputStream fis = new FileInputStream("test.txt");

InputStreamReader isr = new InputStreamReader(fis, "UTF8");

 

, InputStreamReader OutputStreamWriter . , InputStreamReader OutputStreamWriter, getEncoding, :

InputStreamReader defaultReader = new InputStreamReader(fis);

String defaultEncoding = defaultReader.getEncoding();


, . :

1.

2.

3.

import java.io.*;

import java.util.Scanner;

 

public class Reader

{

private static String _encoding;

private static String _string;

 

public static void main(String[] args)

{

if (args.length!= 3)

{

System.out.println("Wrong number of input arguments");

return;

}

 

File testFile = new File(args[0]);

_encoding = args[1];

_string1 = args[2];

 

String contents = GetContents(testFile);

System.out.print("File contents:\r\n" + contents);

System.out.println("--------------------------------------------");

 

Analyze(contents);

}

 

static public String GetContents(File file)

{

StringBuilder contents = new StringBuilder();

 

try

{

if (file == null)

{

throw new IllegalArgumentException("File should not be null.");

}

 

if (!file.exists())

{

throw new FileNotFoundException();

}

 

if (!file.canRead())

{

throw new IllegalArgumentException("File cannot be written: " + file);

}

 

if (!file.isFile())

{

throw new IllegalArgumentException("Should not be a directory: " + file);

}

 

FileInputStream fis = new FileInputStream(file);

InputStreamReader in = new InputStreamReader(fis, _encoding);

BufferedReader input = new BufferedReader(in);

try

{

String line = null;

 

while ((line = input.readLine())!= null)

{

contents.append(line + "\r\n");

}

}

finally

{

input.close();

}

}

catch (FileNotFoundException ex)

{

System.out.println("File does not exist: " + file);

}

catch(IllegalArgumentException ex)

{

System.out.println(ex.getMessage());

}

catch (Exception ex)

{

ex.printStackTrace();

}

 

return contents.toString();

}

 

private static void Analyze(String contents)

{

Scanner scanner = new Scanner(contents);

 

int stringCount = 0;

 

while (scanner.hasNextLine())

{

Scanner s = new Scanner(scanner.nextLine());

 

while (s.hasNext())

{

String word = s.next();

 

if (word.contentEquals(_string))

stringCount++;

}

 

s.close();

}

 

scanner.close();

 

System.out.println(_string + " " + stringCount);

}

}


1. . : n-. ( , ) .

2. . : n-. ; , . .

3. , - ( \ ), . .

4. , ( , , 10 ). .



<== | ==>
| 3. . - ,
:


: 2016-09-03; !; : 857 |


:

:

, , .
==> ...

1779 - | 1463 -


© 2015-2024 lektsii.org - -

: 0.157 .