1.建立一个文本文件读写程序,该程序逐行读取一个文本文件中的所有内容,对于每一行文本将小写字母转换为大写存储到另外一个文本文件中。
作业还是自己做吧。。。。。。
楼主的问题明显带大学作业的风格啊。。
大佬聪明,学渣求教,随便给个注释,或者讲讲这么做呗!表示不会
主要就是io流的使用,好好看看java的IO自然就会了
1.io读取本地文件,然后将文件的内容利用String自带的方法很容易大小写转换,然后再用io流写入本地
2.指明了用ObjectIO 这个开发中不常用,应该也不难,和上面的文件io流应该差别不大
唉,虽然咱上学的时候也是学渣呢,帮你一把,不过你得看明白代码,要不然下次还是一样。
package cn.baokx.traning;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Test {
public static void main(String[] args) {
fun1("D://aaa.txt","D://bbb.txt");
}
public static void fun1(String fromPath,String toPath){
File fromFile = new File(fromPath);
if(!fromFile.exists()) throw new RuntimeException("来源文件不存在:"+fromPath);
if(fromFile.isDirectory()) throw new RuntimeException("来源文件是文件夹:"+fromPath);
File toFile = new File(toPath);
BufferedReader br = null;
BufferedWriter bw = null;
try{
br = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile)));
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile)));
boolean firstLine = true;
String lineStr1 = null;
while ((lineStr1 = br.readLine()) != null) {
if(firstLine){
firstLine = false;
}else{
bw.newLine();
}
bw.write(firstCharToUpperCase(lineStr1));
}
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}finally{
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
//首字母大写
public static String firstCharToUpperCase(String str){
if(str == null) return null;
if(str.length()>1){
char [] charArr = str.toCharArray();
if(charArr[0] >= 'a' && charArr[0] <= 'z'){
charArr[0] = (char) (charArr[0] - ' ');
}
str = new String(charArr);
}
return str;
}
}
package cn.baokx.traning;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
class Student implements Serializable{
private static final long serialVersionUID = 1L;
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "name:"+name+",age:"+age;
}
}
public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student stu = new Student();
stu.setName("张三");
stu.setAge(30);
writeObject("D://stu.obj",stu);
stu = readObject("D://stu.obj");
System.out.println(stu);
}
public static void writeObject(String toPath,Student obj) throws IOException{
File file = new File(toPath);
OutputStream outputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(obj);
objectOutputStream.close();
}
public static Student readObject(String fromPath) throws IOException, ClassNotFoundException{
File file = new File(fromPath);
InputStream inputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
Student stu = (Student)objectInputStream.readObject();
objectInputStream.close();
return stu;
}
}
可以使用工具IOutils啊 很快的
上面的都太复杂了,,人家还是个学生,太残忍了,。。。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/***
**Created by emk on 2017/11/8.
**/
public class TextFile {
public static void main(String []args){
TextFile textFile = new TextFile();
String filePath = "C:/abc/abc.txt";
String result = textFile.readFile(filePath);
System.out.println("读取到的文件中值:"+result);
result = result.toLowerCase();
System.out.println("写入到文件中的值:"+result);
textFile.writeFile(filePath,result);
}
private String readFile(String filePath) {
FileInputStream fileInputStream = null;
String result = "";
try{
fileInputStream = new FileInputStream(filePath);
int size = fileInputStream.available();
byte[] array = new byte[size];
fileInputStream.read(array);
result = new String(array);
}catch (Exception e){e.printStackTrace();}finally {
if (fileInputStream !=null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
public void writeFile(String filePath, String content){
FileOutputStream fileOutputStream = null;
try{
fileOutputStream = new FileOutputStream(filePath);
byte[] array = content.getBytes();
fileOutputStream.write(array);
}catch (Exception e){e.printStackTrace();}finally {
if (fileOutputStream != null){
try{fileOutputStream.close();} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
我也来玩一下
import java.io.*;
import java.util.Arrays;
public class test {**
**public static void main(String[] args) {
String formPath = "F:\temp.txt";
String toPath ="F:\dest.txt";
readFile(formPath,toPath);
}
private static void readFile(String formPath, String toPath) {
try {
BufferedInputStream fis = null;
BufferedOutputStream fos = null;
//输入流
File fromFile = new File(formPath);
fis = new BufferedInputStream(new FileInputStream(fromFile));
//输出流
File toFile = new File(toPath);
fos = new BufferedOutputStream(new FileOutputStream(toFile));
byte[] buffer = new byte[1024];
int temp = 0;
while ((temp=fis.read(buffer))!=-1){
byte[] sbuf = toUpperString(buffer);
fos.write(sbuf);
}
fis.close();
fos.close();
}catch (IOException e){
e.printStackTrace();
}
}
private static byte[] toUpperString(byte[] buffer) {
String sbuffer=new String(buffer).trim().toUpperCase();
byte[] sbuf = sbuffer.getBytes();
return sbuf;
}
}