首页编程java编程java中fis是什么,java中date=fis.read())!=-1什么意思

java中fis是什么,java中date=fis.read())!=-1什么意思

编程之家2023-10-13127次浏览

这篇文章给大家聊聊关于java中fis是什么,以及java中date=fis.read())!=-1什么意思对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。

java中fis是什么,java中date=fis.read())!=-1什么意思

java中date=fis.read())!=-1什么意思

fis应该是FileInputStream的对象。这个涉及IO操作

fis按字节读取某个文件里面的内容(这种情况是是一个字节一个字节的读取,所以你这句话应该是在一个循环里),当读到最后的时候,就会返回-1。

所以如果这是在一个循环里面话,(比如(while(date=fis.read()())!=-1)))所以date=fis.read())!=-1表示一个字节一个字节读这个文件,读到最后一个停止。

java中fis是什么,java中date=fis.read())!=-1什么意思

java编程中Properties类的具体作用和使用!

如果不熟悉 java.util.Properties类,那么现在告诉您它是用来在一个文件中存储键-值对的,其中键和值是用等号分隔的。(如清单 1所示)。最近更新的java.util.Properties类现在提供了一种为程序装载和存储设置的更容易的方法: loadFromXML(InputStreamis)和 storeToXML(OutputStream os, String comment)方法。

一下是详细的说明,希望能给大家带来帮助。

java中fis是什么,java中date=fis.read())!=-1什么意思

清单 1.一组属性示例

foo=bar

fu=baz

将清单 1装载到 Properties对象中后,您就可以找到两个键( foo和 fu)和两个值( foo的 bar和 fu的baz)了。这个类支持带\u的嵌入 Unicode字符串,但是这里重要的是每一项内容都当作 String。

清单2显示了如何装载属性文件并列出它当前的一组键和值。只需传递这个文件的 InputStream给 load()方法,就会将每一个键-值对添加到 Properties实例中。然后用 list()列出所有属性或者用 getProperty()获取单独的属性。

清单 2.装载属性

import java.util.*;

import java.io.*;

public class LoadSample{

public static void main(String args[]) throws Exception{

Properties prop= new Properties();

FileInputStream fis=

new FileInputStream("sample.properties");

prop.load(fis);

prop.list(System.out);

System.out.println("\nThe foo property:"+

prop.getProperty("foo"));

}

}

运行 LoadSample程序生成如清单 3所示的输出。注意 list()方法的输出中键-值对的顺序与它们在输入文件中的顺序不一样。Properties类在一个散列表(hashtable,事实上是一个 Hashtable子类)中储存一组键-值对,所以不能保证顺序。

清单 3. LoadSample的输出

-- listing properties--

fu=baz

foo=bar

The foo property: bar

XML属性文件

这里没有什么新内容。 Properties类总是这样工作的。不过,新的地方是从一个 XML文件中装载一组属性。它的 DTD如清单 4所示。

清单 4.属性 DTD

<?xml version="1.0" encoding="UTF-8"?>

<!-- DTD for properties-->

<!ELEMENT properties( comment?, entry*)>

<!ATTLIST properties version CDATA#FIXED"1.0">

<!ELEMENT comment(#PCDATA)>

<!ELEMENT entry(#PCDATA)>

<!ATTLIST entry key CDATA#REQUIRED>

如果不想细读 XML DTD,那么可以告诉您它其实就是说在外围<properties>标签中包装的是一个<comment>标签,后面是任意数量的<entry>标签。对每一个<entry>标签,有一个键属性,输入的内容就是它的值。清单 5显示了清单 1中的属性文件的 XML版本是什么样子的。

清单 5. XML版本的属性文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM" http://java.sun.com/dtd/properties.dtd">

<properties>

<comment>Hi</comment>

<entry key="foo">bar</entry>

<entry key="fu">baz</entry>

</properties>

如果清单 6所示,读取 XML版本的 Properties文件与读取老格式的文件没什么不同。

清单 6.读取 XML Properties文件

import java.util.*;

import java.io.*;

public class LoadSampleXML{

public static void main(String args[]) throws Exception{

Properties prop= new Properties();

FileInputStream fis=

new FileInputStream("sampleprops.xml");

prop.loadFromXML(fis);

prop.list(System.out);

System.out.println("\nThe foo property:"+

prop.getProperty("foo"));

}

}

关于资源绑定的说明

虽然 java.util.Properties类现在除了支持键-值对,还支持属性文件作为 XML文件,不幸的是,没有内置的选项可以将ResourceBundle作为一个 XML文件处理。是的, PropertyResourceBundle不使用 Properties对象来装载绑定,不过装载方法的使用是硬编码到类中的,而不使用较新的 loadFromXML()方法。

运行清单 6中的程序产生与原来的程序相同的输出,如清单 2所示。

保存 XML属性

新的 Properties还有一个功能是将属性存储到 XML格式的文件中。虽然 store()方法仍然会创建一个类似清单 1所示的文件,但是现在可以用新的 storeToXML()方法创建如清单 5所示的文件。只要传递一个 OutputStream和一个用于注释的 String就可以了。清单 7展示了新的 storeToXML()方法。

清单 7.将 Properties存储为 XML文件

import java.util.*;

import java.io.*;

public class StoreXML{

public static void main(String args[]) throws Exception{

Properties prop= new Properties();

prop.setProperty("one-two","buckle my shoe");

prop.setProperty("three-four","shut the door");

prop.setProperty("five-six","pick up sticks");

prop.setProperty("seven-eight","lay them straight");

prop.setProperty("nine-ten","a big, fat hen");

FileOutputStream fos=

new FileOutputStream("rhyme.xml");

prop.storeToXML(fos,"Rhyme");

fos.close();

}

}

运行清单 7中的程序产生的输出如清单 8所示。

清单 8.存储的 XML文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM" http://java.sun.com/dtd/properties.dtd">

<properties>

<comment>Rhyme</comment>

<entry key="seven-eight">lay them straight</entry>

<entry key="five-six">pick up sticks</entry>

<entry key="nine-ten">a big, fat hen</entry>

<entry key="three-four">shut the door</entry>

<entry key="one-two">buckle my shoe</entry>

</properties>

在这里改了一个例子:

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

/**

*实现properties文件的读取

*@author haoxuewu

*/

public class Test{

public static void main(String[] args){

try{

long start= System.currentTimeMillis();

InputStream is= new FileInputStream("conf.properties");

Properties p= new Properties();

p.load(is);

is.close();

System.out.println("SIZE:"+ p.size());

System.out.println("homepage:"+ p.getProperty("homepage"));

System.out.println("author:"+ p.getProperty("author"));

System.out.println("school:"+ p.getProperty("school"));

long end= System.currentTimeMillis();

System.out.println("Cost:"+(end- start));

} catch(IOException ioe){

ioe.printStackTrace();

}

}

}

conf.properties

# Configuration file

homepage= http://www.blogjava.net/haoxuewu

author= bbflyerwww

school= jilinjianzhugongchengxueyuan

Result

SIZE:3

homepage: http://www.blogjava.net/haoxuewu

author: bbflyerwww

school: jilinjianzhugongchengxueyuan

Java中的actionlistener是什么意思,有什么作用,详细点为好!

actionlistener字面上理解就是动作监听器。

它是一个接口,在实现此接口的类中,你可以给需要关注其动作的组件(如Button)添加监听器(addActionListener(this);),之后在事件处理方法(public void actionPerformed(ActionEvent event){})中,对每个事件进行不同处理。

给你个例子吧,是我自己写的一个记事本:

import java.io.*;

import java.awt.event.*;

import javax.swing.*;

public class MainClass extends JFrame implements ActionListener{

int width= 500,height= 400;

JPanel panel;

JMenuBar bar;

JMenu fileMenu,editMenu,helpMenu;

JMenuItem打开O,新建N,保存S,另存A,剪切T,复制C,粘贴P,关于A;

JTextArea textArea= null;

File tempFile= null;

public MainClass(){//构造方法

setTitle("TextEdit");

setSize(width,height);

panel=(JPanel)getContentPane();

bar= new JMenuBar();

fileMenu= new JMenu("文件F");

fileMenu.setMnemonic('F');

editMenu= new JMenu("编辑E");

editMenu.setMnemonic('E');

helpMenu= new JMenu("帮助H");

helpMenu.setMnemonic('H');

打开O= new JMenuItem("打开O");

打开O.setMnemonic('O');

新建N= new JMenuItem("新建N");

新建N.setMnemonic('N');

保存S= new JMenuItem("保存S");

保存S.setMnemonic('S');

另存A= new JMenuItem("另存A");

另存A.setMnemonic('A');

剪切T= new JMenuItem("剪切C");

剪切T.setMnemonic('t');

复制C= new JMenuItem("复制C");

复制C.setMnemonic('C');

粘贴P= new JMenuItem("粘贴P");

粘贴P.setMnemonic('P');

关于A= new JMenuItem("关于A");

关于A.setMnemonic('A');

fileMenu.add(打开O);

fileMenu.add(新建N);

fileMenu.add(保存S);

fileMenu.add(另存A);

bar.add(fileMenu);

editMenu.add(剪切T);

editMenu.add(复制C);

editMenu.add(粘贴P);

bar.add(editMenu);

helpMenu.add(关于A);

bar.add(helpMenu);

textArea= new JTextArea();

panel.add("North",bar);

panel.add("Center", textArea);

打开O.addActionListener(this);

新建N.addActionListener(this);

保存S.addActionListener(this);

另存A.addActionListener(this);

剪切T.addActionListener(this);

复制C.addActionListener(this);

粘贴P.addActionListener(this);

关于A.addActionListener(this);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public void actionPerformed(ActionEvent event){//处理事件

if(event.getSource()==打开O){//处理打开

JFileChooser jfc= new JFileChooser();

jfc.showOpenDialog(panel);

tempFile= jfc.getSelectedFile();

FileReader fis;

try{

fis= new FileReader(tempFile);

textArea.read(fis,null);

textArea.setEditable(true);

}catch(Exception ex){ex.printStackTrace();}

}

if(event.getSource()==新建N){//处理新建

textArea.setEditable(true);

textArea.setText(null);

}

if(event.getSource()==保存S){//处理保存

if(tempFile== null){

JFileChooser jfc= new JFileChooser();

jfc.showSaveDialog(panel);

tempFile= jfc.getSelectedFile();

try{

FileWriter fos= new FileWriter(tempFile);

textArea.write(fos);

}catch(Exception ex){ex.printStackTrace();}

}

else{

try{

FileWriter fos= new FileWriter(tempFile);

textArea.write(fos);

}catch(Exception ex){ex.printStackTrace();}

}

}

if(event.getSource()==另存A){//处理另存

JFileChooser jfc= new JFileChooser();

jfc.showSaveDialog(panel);

tempFile= jfc.getSelectedFile();

try{

FileWriter fos= new FileWriter(tempFile);

textArea.write(fos);

}catch(Exception ex){ex.printStackTrace();}

}

if(event.getSource()==剪切T){//处理剪切

textArea.cut();

}

if(event.getSource()==复制C){//处理复制

textArea.copy();

}

if(event.getSource()==粘贴P){//处理粘贴

textArea.paste();

}

if(event.getSource()==关于A){//处理关于

textArea.setText("Manifest-Version: 1.0\n"+

"Created-By: Libra_JL\n"+

"QQ: 254791521\n");

textArea.setEditable(false);

panel.validate();

validate();

}

}

public static void main(String []args){//主函数

new MainClass();

}

}

END,本文到此结束,如果可以帮助到大家,还望关注本站哦!

贷款利息计算(贷款利息计算器)java对象的组成有什么作用是什么意思?java对象的作用