62,614
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
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);
}
}