递归算法是把问题转化为规模缩小了的同类问题的子问题。然后递归调用函数(或过程)来表示问题的解。
一个过程(或函数)直接或间接调用自己本身,这种过程(或函数)叫递归过程(或函数).
创建Calculate类
//计算1+...+1000的值,不使用循环语句
public class Calculate {
private int i = 1;
public int sum=0;
public void add(){
if(i<=1000){
sum+=i;
i++;
add();
}
}
}
创建Test类
public class Test {
public static void main(String[] args) {
Calculate calculate = new Calculate();
calculate.add();
System.out.println(calculate.sum);
}
}
要求:编写程序实现整数的四则运算,输出结果!
创建MyDigitStack类
public class MyDigitStack {
private int array[]=new int[20];
private int index=-1;
public void push(int i){//压栈
index++;
array[index]=i;
}
public int pop(){//出栈
int j=array[index];
index--;
return j;
}
public int getTop(){//得到栈顶的数据
return array[index];
}
public int getIndex(){//得到索引
return index;
}
}
创建MyCharStack类
public class MyCharStack {
private char array[]=new char[20];
private int index=-1;
public void push(char i){//压栈
index++;
array[index]=i;
}
public char pop(){//出栈
char j=array[index];
index--;
return j;
}
public char getTop(){//得到栈顶的数据
return array[index];
}
public int getIndex(){//得到索引
return index;
}
}
创建Calculate类
//输入一个式子,算出计算结果并输出
import java.util.Scanner;
public class Calculate {
public static void main(String[] args) {
System.out.println("请输入算术式:");
Scanner sc = new Scanner(System.in);
String s = sc.next();
MyDigitStack shuzi = new MyDigitStack();
MyCharStack fuhao = new MyCharStack();
String digit = "";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
digit += c;// 将两位数及以上的数字连接起来
} else {
shuzi.push(Integer.parseInt(digit));
if (c == '=') {
int after = shuzi.pop();
int before = shuzi.pop();
char top = fuhao.pop();
int result = yunsuan(before, top, after);
if (top=='+'||top=='-') {
System.out.println(result);
} else {
shuzi.push(result);
int after1 = shuzi.pop();
int before1 = shuzi.pop();
char top1 = fuhao.pop();
int result1 = yunsuan(before1, top1, after1);
System.out.println(result1);
}
} else {
digit = "";
if (fuhao.getIndex() == -1) {
fuhao.push(c);
} else {
jisuan(shuzi, fuhao, c);
}
}
}
}
}
//写一个具体运算过程的方法
private static void jisuan(MyDigitStack shuzi, MyCharStack fuhao, char c) {
if (fuhao.getIndex() == -1) {
fuhao.push(c);
return;
}
char top = fuhao.getTop();
if (bijiao(c, top)) {
fuhao.push(c);
} else {
top = fuhao.pop();
int after = shuzi.pop();
int before = shuzi.pop();
int result = yunsuan(before, top, after);
shuzi.push(result);
jisuan(shuzi, fuhao, c);
}
}
//写一个比较运算符号优先级的方法
public static boolean bijiao(char c, char top) {
if (c == '*' || c == '/') {
if (top == '*' || top == '/') {
return false;
}
return true;
}
return false;
}
//写一个四则运算的静态方法
public static int yunsuan(int before, char c, int after) {
int a = 0;
switch (c) {
case '+':
a = before + after;
break;
case '-':
a = before - after;
break;
case '/':
a = before / after;
break;
case '*':
a = before * after;
break;
default:
break;
}
return a;
}
}
找到D盘中所有以“.txt”结尾的文件,并输出文件的绝对路径。
public class Bianli {
public static void main(String[] args) {
erdogicFile("d:\\");//C盘不让读!
}
public static void erdogicFile(String filePath){
File file = new File(filePath);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if(files[i].isDirectory()){
erdogicFile(files[i].getAbsolutePath());
//如果是文件夹,遍历
}else{
if(files[i].getName().endsWith(".txt")){
System.out.println(files[i].getAbsolutePath());
}
}
}
}
}
运行结果:
另一种写法是利用过滤器FilenameFilter接口
创建TxtFilter类实现FilenameFilter接口
import java.io.File;
import java.io.FilenameFilter;
public class TxtFilter implements FilenameFilter{
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".txt");
}
}
创建Test类
import java.io.File;
public class Test {
public static void main(String[] args) {
TxtFilter txtFilter = new TxtFilter();
File file = new File("d:\\");
File[] files =file.listFiles(txtFilter);
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
}
}
运行结果:
注意:使用FilenameFilter接口来过滤想得到的文件时,它不会进入一个文件夹内部!
InputStream类是一个抽象类,是表示字节输入流的所有类的超类。
读系统的一个文件,并打印出来
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import lz20150721.d1.Test;
public class Demo {
public static void main(String[] args) {
File f = new File("d://Test.txt");
InputStream is = null;
try {
byte[] array = new byte[1024];
is = new FileInputStream(f);
try {
int index = is.read(array);
while (index != -1) {
System.out.println(new String(array,0,index));
index = is.read(array);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
将一段内容写入一个指定的文件里边
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
System.out.println("请输入您想写入的内容:");
Scanner sc = new Scanner(System.in);
String s = sc.next();
File file = new File("d:\\11.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(s.getBytes());// 将输入的String内容转换为Byte数组写入
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
将一个文件里边的内容复制到另一个文件里边(使用FileInputStream和FileOutputStream)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//copy文件
public class Demo {
public static void main(String[] args) {
copy("d:11.txt","d:22.txt");
}
public static void copy(String a,String b) {
File sourceFile = new File(a);
File destinationFile = new File(b);
if (!sourceFile.exists()) {
System.out.println("对不起,您要复制的文件不存在!");
}
if (!destinationFile.exists()) {
try {
destinationFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(sourceFile);
out = new FileOutputStream(destinationFile);
byte[] array = new byte[1024];
int i = 0;
while (i != -1) {
i = in.read(array);
if (i != -1) {
out.write(array,0,i);
}
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
BufferedReader(Reader in)
创建一个使用默认大小输入缓冲区的缓冲字符输入流。
读取指定文件里的内容,并打印出来
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Demo {
public static void main(String[] args) {
File file = new File("d:\\11.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String s = br.readLine();
while (s != null) {
System.out.println(s);
s = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
BufferedWriter(Writer out)
创建一个使用默认大小输出缓冲区的缓冲字符输出流。
将一段内容写入指定的文件内
import java.io.BufferedReader;
import 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.OutputStream;
import java.io.OutputStreamWriter;
public class Demo {
public static void main(String[] args) {
File file = new File("d:\\33.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
bw.write("我写了一段话!");
bw.newLine();//换行
bw.write("我又写了一段话!");
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
利用PrintStream类修改标准输出方式,将本来要打印到控制台的内容读入到指定文件里边。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
public class Demo {
public static void main(String[] args) {
File file = new File("d:\\44.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
PrintStream ps = new PrintStream(file);
System.setOut(ps);
System.out.println("您好,我修改了你的标准输出方式!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
XML:可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。它是对超文本标记语言(HTML)的补充。
格式:
<?xml version="1.0" encoding="UTF-8"?>
<!-- published at 2015-07-23 15:36:32 -->
<Profiles>
<Weather id="1" name="天气">
<city>北京</city>
<status1>多云</status1>
<status2>雷阵雨</status2>
<figure1>duoyun</figure1>
<figure2>leizhenyu</figure2>
<direction1>无持续风向</direction1>
<direction2>无持续风向</direction2>
<power1>≤3</power1>
<power2>≤3</power2>
<temperature1>32</temperature1>
<temperature2>21</temperature2>
<ssd>8</ssd>
<tgd1>28</tgd1>
<tgd2>28</tgd2>
<zwx>1</zwx>
<ktk>3</ktk>
<pollution>3</pollution>
<xcz>5</xcz>
<zho></zho>
<chy>1</chy>
<chy_shuoming>短袖衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫</chy_shuoming>
<pollution_l>轻度</pollution_l>
<zwx_l>最弱</zwx_l>
<ssd_l>较热</ssd_l>
<fas_l>暂无</fas_l>
<chy_l>薄短袖类</chy_l>
<ktk_l>较适宜开启(制冷)</ktk_l>
<xcz_l>不适宜</xcz_l>
<diy_l>暂无</diy_l>
<pollution_s>对空气污染物扩散无明显影响</pollution_s>
<zwx_s>紫外线最弱</zwx_s>
<ssd_s>户外活动不适宜在中午前后展开。</ssd_s>
<ktk_s>比较适宜开启空调</ktk_s>
<xcz_s>洗车后当日有降水、大风或沙尘天气,不适宜洗车</xcz_s>
<gm>1</gm>
<gm_l>低发期</gm_l>
<gm_s>环境温度较高,要提防长时间在空调环境中引发的空调病;</gm_s>
<yd>5</yd>
<yd_l>不适宜</yd_l>
<yd_s>出现下雨天气时会伴有雷声,不适宜户外运动;</yd_s>
<savedate_weather>2015-07-23</savedate_weather>
<savedate_life>2015-07-23</savedate_life>
<savedate_zhishu>2015-07-23</savedate_zhishu>
<udatetime>2015-07-23 08:10:12</udatetime>
</Weather>
</Profiles>
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Demo {
public static void main(String[] args) {
File file = new File("d:\\55.txt");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db= dbf.newDocumentBuilder();
Document doc = db.parse(file);
NodeList weather = doc.getElementsByTagName("Weather");
for (int i = 0; i < weather.getLength(); i++) {
Node weatherChild = weather.item(i);
NamedNodeMap map=weatherChild.getAttributes();
String s1=map.getNamedItem("id").getNodeValue();
String s2=map.getNamedItem("name").getNodeValue();
System.out.println("id="+s1+" name="+s2);
for (Node weatherChildNode=weatherChild.getFirstChild(); weatherChildNode!=null; weatherChildNode=weatherChildNode.getNextSibling()) {
if(weatherChildNode.getNodeType()==Node.ELEMENT_NODE){
String name=weatherChildNode.getNodeName();
if(weatherChildNode.getFirstChild()!=null){
String value = weatherChildNode.getFirstChild().getNodeValue();
System.out.println(name+":"+value);
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
代码实现
创建MySAXHandler类
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
//创建MySAXHandler类继承DefaultHandler类
public class MySAXHandler extends DefaultHandler{
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
System.out.println("开始文档");
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
System.out.println("结束文档");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
System.out.println("开始标签"+qName);
if(qName=="Weather"){
System.out.println("Attributes"+attributes.getValue("name"));
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
System.out.println("结束标签"+qName);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
System.out.println("标签内容"+new String(ch,start,length));
}
}
创建Test类
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class Test {
public static void main(String[] args) {
MySAXHandler handler= new MySAXHandler();
File file = new File("d:\\55.txt");
SAXParserFactory factory=SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
parser.parse(file, handler);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}