java.io.Reader
and Writer
:
Work on character streams - that is, treat everything like it's going to be a character.
java.io.InputStream
and OutputStream
:
Work on byte streams - that is, treat everything like it's binary data.
Reader
and Writer
.
FileReader
: for reading files.
FileWriter
: for writing files.
FileReader fr = null;
File f = new File("myFile.txt");
try {
fr = new FileReader (f);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
try {
char char1 = fr.read();
// Read a char at a time
fr.close();
// Close the connection to
// the file so others can use it.
} catch (IOException ioe) {
ioe.printStackTrace();
}
FileWriter fw = null;
File f = new File("myFile.txt");
try {
fw = new FileWriter (f, true);
//Note optional boolean sets whether
// to append to the file (true) or
// overwrite it (false). Default is overwrite.
} catch (IOException ioe) {
ioe.printStackTrace();
}
try {
fw.write("A");
fw.flush();
// Make sure everything in
//the stream is written out.
fw.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
BufferedReader br = new BufferedReader(fr);
// Remember fr is a FileReader not a File.
// Run through the file once to count the lines
// and make a String array the right size.
int lines = -1;
String textIn = " ";
String[] file = null;
try {
while (textIn != null) {
textIn = br.readLine();
lines++;
}
file = new String[lines];
// close and remake the file reader / buffer
// here to set it back to the file start.
//Go back to the start of the file
// and read it into the array.
for (int i = 0; i < lines; i++) {
file[i] = br.readLine();
}
br.close();
} catch (IOException ioe) {}
BufferedWriter bw = new BufferedWriter (fw);
// Remember fw is a FileWriter not a File.
String[][] strData = getStringArray();
try{
for (int i = 0; i < strData.length; i++) {
for (int j = 0; j < strData[i].length; j++) {
bw.write(strData[i][j] + ", ");
}
bw.newLine();
}
bw.close();
} catch (IOException ioe) {}
String line = "Call me Dave";
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Call
me
Dave
Default separators: space, tab, newline, carriage-return character, and form-feed.
double d = Double.parseDouble("0.5");
int i = Integer.parseInt("1");
boolean b = Boolean.parseBoolean("true");
String str = String.valueOf(0.5);
String str = String.valueOf(data[i][j]);
for (int i = 0; i <= lines; i++) {
file[i] = br.readln();
}
br.close();
double[][] data = new double [lines][];
for (int i = 0; i < lines; i++) {
StringTokenizer st =
new StringTokenizer(file[i],", ");
// Comma and space separated data
data[i] = new double[st.countTokens()];
int j = 0;
while (st.hasMoreTokens()) {
data[i][j] =
Double.parseDouble(st.nextToken());
j++;
}
}
BufferedWriter bw = new BufferedWriter (fw);
double[][] dataIn = getdata();
String tempStr = "";
try {
for (int i = 0; i < dataIn.length; i++) {
for (int j = 0; j < dataIn[i].length; j++) {
tempStr = String.valueOf(dataIn[i][j]);
//Converts the double to a String.
bw.write(tempStr + ", ");
}
bw.newLine();
}
bw.close();
} catch (IOException ioe) {}
Scanner s = null;
try {
s = new Scanner(
new BufferedReader(
new FileReader("myText.txt")));
while (s.hasNext()) {
System.out.println(s.next());
}
if (s != null) {
s.close();
}
} catch (Exception e) {}
However, no token counter, so not great for reading into arrays.
s.useDelimiter(",\\s*");
s.next() / s.hasNext() String
nextBoolean() / hasNextBoolean() boolean
nextDouble() / hasNextDouble() double
nextInt() / hasNextInt() int
nextLine() / hasNextLine() String
InputMismatchException
.
Scanner s = new Scanner(System.in);
int i = s.nextInt();
String str = s.nextLine();
startsWith(String prefix)
, endsWith(String suffix)
indexOf(int ch)
, indexOf(int ch, int fromIndex)
indexOf(String str)
,
indexOf(String str, int fromIndex)
lastIndexOf
replace(char oldChar, char newChar)
substring(int beginIndex, int endIndex)
,
substring(int beginIndex)
toLowerCase()
, toUpperCase()
trim()
String str = "old pond; frog leaping; splash";
int start = str.indexOf("leaping");
int end = str.indexOf(";", start);
String startStr = str.substring(0, start);
String endStr = str.substring(end);
str = startStr + "jumping" + endStr;