package com.dao;import java.io.*;public class GoonieResult {
public static void readFileByChars(String fileName){
File file = new File(fileName);
System.out.println(fileName+"=====");
Reader reader = null;
try{
System.out.println("By chars");
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while((charread = reader.read(tempchars))!=-1){
if((charread == tempchars.length)&&(tempchars[tempchars.length -1]!='\r')){
System.out.print(tempchars);
}else{
for(int i=0; i<charread; i++){
if(tempchars[i] == '\r'){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}
}catch(Exception e1){
e1.printStackTrace();
}finally{
if(reader != null){
try{
reader.close();
}catch(IOException e){
}
}
}
}
public static void main(String args[]){
String fileName = "D:\\test.txt";
GoonieResult.readFileByChars(fileName);
}
}D:\test.txt=====
By chars
java.io.FileNotFoundException: D:\test.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.dao.GoonieResult.readFileByChars(GoonieResult.java:14)
at com.dao.GoonieResult.main(GoonieResult.java:43)
public static void readFileByChars(String fileName){
File file = new File(fileName);
System.out.println(fileName+"=====");
Reader reader = null;
try{
System.out.println("By chars");
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while((charread = reader.read(tempchars))!=-1){
if((charread == tempchars.length)&&(tempchars[tempchars.length -1]!='\r')){
System.out.print(tempchars);
}else{
for(int i=0; i<charread; i++){
if(tempchars[i] == '\r'){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}
}catch(Exception e1){
e1.printStackTrace();
}finally{
if(reader != null){
try{
reader.close();
}catch(IOException e){
}
}
}
}
public static void main(String args[]){
String fileName = "D:\\test.txt";
GoonieResult.readFileByChars(fileName);
}
}D:\test.txt=====
By chars
java.io.FileNotFoundException: D:\test.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.dao.GoonieResult.readFileByChars(GoonieResult.java:14)
at com.dao.GoonieResult.main(GoonieResult.java:43)
改为String fileName = "D:\\test.txt.txt";
或者文件重命名为test.
这个文件不存在吧
看看是不是文件名写错了啊
我注意到了这行,你是不是要生成这个文件后直接读取?
你在这行下面执行下file.getPAth或者getName之类的方法,打印出来看看。
怀疑文件生成的不对。
执行下file.getPAth或者getName之类的方法,打印出来看看。
package com.dao;import java.io.*;public class GoonieResult {
public static void readFileByChars(String fileName) {
File file = new File(fileName);
System.out.println(fileName + "=====");
Reader reader = null;
try {
System.out.println("By chars");
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while ((charread = reader.read(tempchars)) != -1) {
if ((charread == tempchars.length) &&
(tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
} public static void main(String args[]) {
/* 1.确定机器上是否真的存在此文本文件
2.机器配置的后缀名是不是被隐藏了
*/
String fileName = "D:\\test.txt";
GoonieResult.readFileByChars(fileName);
}
}
public static void readFileByChars(String fileName) {
File file = new File(fileName);
System.out.println(fileName + "=====");
Reader reader = null;
try {
System.out.println("By chars");
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while ((charread = reader.read(tempchars)) != -1) {
if ((charread == tempchars.length) &&
(tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
} public static void main(String args[]) {
String fileName = "D:\\test.txt";
GoonieResult.readFileByChars(fileName);
}
}
正确方式 D:/test.txt
*1、 D:\test.txt 文件指示方式错了 ,正确方式 D:/test.txt*2、该文件要存在,如果不存在,会抛出异常import java.io.*; public class GoonieResult {
public static void readFileByChars(String fileName){
File file = new File(fileName);
System.out.println(fileName+"=====");
Reader reader = null;
try{
System.out.println("By chars");
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while((charread = reader.read(tempchars))!=-1){
if((charread == tempchars.length)&&(tempchars[tempchars.length -1]!='\r')){
System.out.print(tempchars);
}else{
for(int i=0; i <charread; i++){
if(tempchars[i] == '\r'){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}
}catch(Exception e1){
e1.printStackTrace();
}finally{
if(reader != null){
try{
reader.close();
}catch(IOException e){ }
}
}
} public static void main(String args[]){
String fileName = "D:/test.txt";
GoonieResult.readFileByChars(fileName);
}
}