📄 stream.java
字号:
package za.co.halo.SecureCommunications.util;
import java.io.*;
import java.util.*;
import java.text.*;
/**
* This class allows files to be read and writen too.
*/
public class Stream
{
public static final int READ = 0, WRITE = 1;
private static DecimalFormat N = new DecimalFormat();
private static final String spaces = " ";
private BufferedReader in;
private PrintWriter out;
private String S;
private StringTokenizer T;
/**
* This constructor receives an filename and either
* opens it or creates it depending on the second
* paramenter
*/
public Stream(File file, int how) throws FileNotFoundException, IOException
{
switch (how)
{
case READ:
in = open(file);
break;
case WRITE:
out = create(file);
break;
}
}
/**
* This constructor receives an input stream and opens
* it so the package can use it
*/
public Stream(InputStream i)
{
in = open(i);
}
/**
* This constructor receives an filename and either
* opens it or creates it depending on the second
* paramenter
*/
public Stream(String filename, int how) throws FileNotFoundException, IOException
{
switch (how)
{
case READ:
in = open(new File(filename));
break;
case WRITE:
out = create(new File(filename));
break;
}
}
public static String format(double number, int align, int frac)
{
N.setGroupingUsed(false);
N.setMaximumFractionDigits(frac);
N.setMinimumFractionDigits(frac);
String num = N.format(number);
if (num.length() < align)
num = spaces.substring(0, align - num.length()) + num;
return num;
}
public static String format(int number, int align)
{
N.setGroupingUsed(false);
N.setMaximumFractionDigits(0);
String num = N.format(number);
if (num.length() < align)
num = spaces.substring(0, align - num.length()) + num;
return num;
}
public void close() throws IOException
{
if (out != null)
out.close();
if (in != null)
in.close();
}
public void flush()
{
out.flush();
}
public void print(char s)
{
out.print(s);
out.flush();
}
public void print(double s)
{
out.print(s);
out.flush();
}
public void print(int s)
{
out.print(s);
out.flush();
}
public void print(Object s)
{
out.print(String.valueOf(s));
out.flush();
}
public void print(String s)
{
out.print(s);
out.flush();
}
public void println(char s)
{
out.println(s);
out.flush();
}
public void println(double s)
{
out.println(s);
out.flush();
}
public void println(int s)
{
out.println(s);
out.flush();
}
public void println(Object s)
{
out.println(String.valueOf(s));
out.flush();
}
public void println(String s)
{
out.println(s);
out.flush();
}
/** It returns the char from the input stream */
public char readChar() throws IOException
{
if (T == null)
refresh();
while (true)
{
try
{
return T.nextToken().trim().charAt(0);
}
catch (NoSuchElementException e1)
{
refresh();
}
}
}
/** It returns the double from the input stream */
public double readDouble() throws IOException
{
if (T == null)
refresh();
while (true)
{
try
{
String item = T.nextToken();
return Double.valueOf(item.trim()).doubleValue();
}
catch (NoSuchElementException e1)
{
refresh();
}
catch (NumberFormatException e2)
{
e2.printStackTrace();
// Bug.pr("Error in number, try again.");
}
}
}
/** It returns the int from the input stream */
public int readInt() throws IOException
{
if (T == null)
refresh();
while (true)
{
try
{
return Integer.parseInt(T.nextToken());
}
catch (NoSuchElementException e1)
{
refresh();
}
catch (NumberFormatException e2)
{
System.out.println("Error in number, try again.");
}
}
}
/**
* It returns the next line from the stream. It stops
* reading at the '\n' character
*/
public String readLine() throws IOException
{
refresh();
return S;
}
/**
* It returns the string from the input stream. It stops
* at the first white space.
*/
public String readString() throws IOException
{
if (T == null)
refresh();
while (true)
{
try
{
return T.nextToken();
}
catch (NoSuchElementException e1)
{
refresh();
}
}
}
/**
* This method returns a PrintWriter class linked to the
* filename specified
*/
private PrintWriter create(File filename) throws IOException
{
return new PrintWriter(new FileWriter(filename));
}
/** This method opens the filename given to it */
private BufferedReader open(File filename) throws FileNotFoundException
{
return new BufferedReader(new FileReader(filename));
}
/** This method opens the input stream given to it */
private BufferedReader open(InputStream in)
{
return new BufferedReader(new InputStreamReader(in));
}
/** It refreshes the input */
private void refresh() throws IOException
{
S = in.readLine();
if (S == null)
throw new EOFException();
T = new StringTokenizer(S);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -