Java操作文本封装类

Python011

Java操作文本封装类,第1张

import java io BufferedReader

import java io BufferedWriter

import java io File

import java io FileReader

import java io FileWriter

import java io IOException

import java util ArrayList

import java util List

/**

* 用于对记事本的操作

*

* @author 沙琪玛

*

*/

public class NoteOperate {

// txt文件路径

private String filePath

/**

* 构造函数

*

* @param filePath

*            文本文件全路径

*/

public NoteOperate(String filePath) {

this filePath = filePath

}

/**

* 构造函数

*

* @param file

*            需要读取的文本文件

*/

public NoteOperate(File file) {

this filePath = file getPath()

}

/**

* 判断文本文件是否存在

*

* @return 存在返回true 否则返回false

*/

public boolean exists() {

File file = new File(this filePath)

return file exists()

}

/**

* 得到这个txt所有的列的数据 空行将自动跳过 并自动删除文字前后的空格

*

* @return List<String>

* @throws IOException

*/

public List<String>fileLinesContent() throws IOException {

List<String>strs = new ArrayList<String>()

File file = new File(this filePath)

FileReader fr = new FileReader(file)// 建立FileReader对象 并实例化为fr

BufferedReader br = new BufferedReader(fr)// 建立BufferedReader对象 并实例化为br

String Line = br readLine()// 从文件读取一行字符串

// 判断读取到的字符串是否不为空

while (Line != null) {

if (! equals(Line))

strs add(Line trim())

Line = br readLine()// 从文件中继续读取一行数据

}

br close()// 关闭BufferedReader对象

fr close()// 关闭文件

return strs

}

/**

* 创建一个空的记事本文档 如果这个记事本文档存在就不再创建 函数还未写实现部分<br/>如果文本已经存在则不再创建

*

* @throws IOException

*/

public void createEmptyNote() throws IOException {

File file = new File(this filePath)

if (!file exists())

file createNewFile()

}

/**

* 将内容写入这个文本 注意以前的内容将会被删除

*

* @param str

*            将要写入的内容

* @throws IOException

*/

public void writeString(String str) throws IOException {

File file = new File(this filePath)

BufferedWriter output = new BufferedWriter(new FileWriter(file))

output write(str)

output close()// 关闭BufferedReader对象

}

/**

* 在文本的指定行插入文字 如果该行已经存在 该行内容将会被删除 如果行号不存在 将会 *** 入到最后一行

*

* @param i

*            行号 行号为 时 将插入到最后一行

* @param str

*            将要插入的内容

* @throws IOException

*/

public void insertWords(int i String str) throws IOException {

List<String>strs = fileLinesContent()

// 进行插入操作

if (i == || strs size() <i) // 插入到最后一行

{

strs add(str)

} else { // 插入到文本中

strs set(i str)

}

// 重新写入到文本

StringBuffer *** = new StringBuffer()

for (String temp : strs) {

*** append(temp replace( \r\n )+ \r\n )

}

writeString( *** toString())

}

lishixinzhi/Article/program/Java/hx/201311/26945

没有纯文本语言的说法,只能说是纯编程语言。

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念。Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

发展:

2006年11月13日,Java技术的发明者Sun公司宣布,将Java技术作为免费软件对外发布。Sun公司正式发布的有关Java平台标准版的第一批源代码,以及Java迷你版的可执行源代码。从2007年3月起,全世界所有的开发人员均可对Java源代码进行修改。

2009年,甲骨文公司宣布收购Sun。2010年,Java编程语言的共同创始人之一詹姆斯·高斯林从Oracle公司辞职。2011年,甲骨文公司举行了全球性的活动,以庆祝Java7的推出,随后Java7正式发布。2014年,甲骨文公司发布了Java8正式版。

import java.awt.*\x0d\x0aimport java.awt.event.*\x0d\x0aimport javax.swing.*\x0d\x0aimport java.io.*\x0d\x0a\x0d\x0apublic class f1 extends Frame implements ActionListener\x0d\x0a{\x0d\x0aprivate MenuBar menubar=new MenuBar()\x0d\x0aprivate Menu filemenu=new Menu("文件")\x0d\x0aprivate Menu editmenu=new Menu("编辑")\x0d\x0aprivate Menu formmenu=new Menu("格式")\x0d\x0aprivate MenuItem[] itemf=new MenuItem[4]\x0d\x0aprivate MenuItem[] iteme=new MenuItem[6]\x0d\x0aprivate MenuItem[] items=new MenuItem[2]\x0d\x0aprivate TextArea tf=new TextArea()\x0d\x0a\x0d\x0apublic int a=0,b=0,c=0,style=Font.PLAIN,size=15\x0d\x0apublic String s1="red:"+a+" "+"green:"+b+" "+"blue"+c,\x0d\x0as2="宋体"\x0d\x0a\x0d\x0apublic String[] sz1={"10","16","24","30","32","36"},\x0d\x0asz2={"宋体","黑体","幼圆","隶书","行楷","Arial","Georgia"},\x0d\x0asz3={"粗体","倾斜","常规","粗斜"}\x0d\x0a\x0d\x0aJDialog dialog=new JDialog(this,"字体",true)\x0d\x0aContainer cp=dialog.getContentPane()\x0d\x0aJLabel[] lb=new JLabel[8]\x0d\x0aJLabel lb1=new JLabel(s1,JLabel.LEFT)\x0d\x0aJButton b1=new JButton("确定"),\x0d\x0ab2=new JButton("取消")\x0d\x0aJComboBox jc1=new JComboBox(),\x0d\x0ajc2=new JComboBox(),\x0d\x0ajc3=new JComboBox()\x0d\x0aJScrollBar jb1=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260)\x0d\x0aJScrollBar jb2=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260)\x0d\x0aJScrollBar jb3=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260)