代码如下:
import java.io.BufferedReaderimport java.io.BufferedWriter
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStreamReader
import java.io.OutputStreamWriter
public class App {
public static void main(String[] args) throws FileNotFoundException, IOException {
try (
FileInputStream inputStream = new FileInputStream(new File("d:\\temp\\abc.txt"))
InputStreamReader streamReader = new InputStreamReader(inputStream)
BufferedReader reader = new BufferedReader(streamReader)
FileOutputStream outputStream = new FileOutputStream(new File("d:\\temp\\output.txt"))
OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream)
BufferedWriter writer = new BufferedWriter(streamWriter)
) {
int count = 1
String line = null
while ((line = reader.readLine()) != null) {
writer.write(count++ + ": " + line)
writer.newLine()
}
}
System.out.println("done.")
}
}
大概就是这样啦,换成你自己的文件名就行了import java.io.FileNotFoundException
import java.io.IOException
import java.io.RandomAccessFile
import java.util.ArrayList
public class ShowSourceCode {
public static void main(String[] args) throws FileNotFoundException, IOException{
RandomAccessFile file = new RandomAccessFile("Add.java", "r")
String tmp
while((tmp = file.readLine()) != null){
System.out.println(tmp)
}
file.seek(0)
int lineNum = 1
while((tmp = file.readLine()) != null){
System.out.println(lineNum + ": " + tmp)
lineNum++
}
}
}
import java.io.*import java.util.*
public class Test{
public static void main(String[] args){
List<String>list=new ArrayList<String>()
try {
File f=new File("d:/file/hello.txt ")
Scanner sc=new Scanner(f)
int k=0
while(sc.hasNextLine()){
list.add(++k+" "+sc.nextLine())
}
FileWriter fw=new FileWriter(f)
for(int i=0i<list.size()i++)
fw.write(list.get(i)+"\r\n")
fw.flush()
fw.close()
System.out.println(" *** 作已经成功完成!")
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)