1,file class
Represents the name of a particular file and the name of a group of files under a directory. If it points to a set of files, you can call the list () method with the collection of words and return an array of characters.
Example 1
Package filetest;
Import Java.io.File;
public class Dirlist {
/**
* @param args */public
static void Main (string[] args) {
//TODO Auto-gene Rated method stub
file Filedir=new file ("/home/hadoop/data/lucenedata");
File[] Textfiles=filedir.listfiles ();
for (File file:textfiles) {
System.out.println (File.getabsolutepath ());
System.out.println (File.getname ());}}
Results
/home/hadoop/data/lucenedata/txt1.txt
txt1.txt
/home/hadoop/data/lucenedata/txt2.txt
txt2.txt
/home/hadoop/data/lucenedata/txt5.txt
txt5.txt
/home/hadoop/data/lucenedata/txt4.txt
Txt4.txt
/home/hadoop/data/lucenedata/txt3.txt
txt3.txt
Absolute path and filename of output file
2, input and output
Buffer input File
Package inoutput;
Import Java.io.BufferedReader;
Import Java.io.FileReader;
Import Java.io.IOError;
Import java.io.IOException;
public class Bufferedinputfile {
/**
* @param args
*
/public static String read (String filename) throws I oexception{
BufferedReader in=new bufferedreader (new FileReader (filename));
String s;
StringBuilder SB =new StringBuilder ();
while ((S=in.readline ())!=null) {
sb.append (s+ "\ n");
}
In.close ();
return sb.tostring ();
}
public static void Main (string[] args) throws IOException {
//TODO auto-generated method stub
System.out.print ln (Read ("./src/filetest/dirlist.java"));
}
Results
Package filetest;
Import Java.io.File;
Import java.io.IOException;
public class Dirlist {
/**
* @param args
* @throws ioexception
/public static void Main (string[) AR GS) throws IOException {
//TODO auto-generated method stub
File Filedir=new file ("/home/hadoop/data/lucenedata ");
File[] Textfiles=filedir.listfiles ();
for (File file:textfiles) {
System.out.println (File.getabsolutepath ());
System.out.println (File.getcanonicalpath ());
System.out.println (File.getname ());}}
Input from memory
Package inoutput;
Import java.io.IOException;
Import Java.io.StringReader;
public class Memoryinput {
/**
* @param args
* @throws ioexception
/public static void main (String [] args) throws IOException {
//TODO auto-generated method stub
StringReader inreader=new StringReader ( Bufferedinputfile.read ("./src/filetest/dirlist.java"));
int C;
while ((C=inreader.read ())!=-1) {
System.out.println ((char) c);
}
System.out.println (Bufferedinputfile.read ("./src/filetest/dirlist.java"));
}
Results
Package inoutput;
Import java.io.IOException;
Import Java.io.StringReader;
public class Memoryinput {
/**
* @param args
* @throws ioexception
/public static void main (String [] args) throws IOException {
//TODO auto-generated method stub
StringReader inreader=new StringReader ( Bufferedinputfile.read ("./src/filetest/dirlist.java"));
int C;
while ((C=inreader.read ())!=-1) {
System.out.println ((char) c);
}
System.out.println (Bufferedinputfile.read ("./src/filetest/dirlist.java"));
}
Basic file output
Package inoutput;
Import Java.io.BufferedReader;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Java.io.StringReader;
public class Basicfileoutput {
static String file = ' output.out ';
/**
* @param args */public
static void Main (string[] args) throws ioexception{
//TODO auto-generated Method stub
BufferedReader in= new BufferedReader (Bufferedinputfile.read ("./src/filetest/ Dirlist.java "));
PrintWriter out=new printwriter (file);
int linecount=1;
String s;
while ((S=in.readline ())!=null) {
out.println (linecount++ + ":" +s);
}
Out.close ();
System.out.println (bufferedinputfile.read (file));//Read System.out.println from memory
(Bufferedinputfile.read ("./ Output.out "));
}
Results
1:package filetest;
2:3: Import java.io.File;
4:import java.io.IOException; 5:6: public class Dirlist {7:8:/** 9: * @param args: * @throws ioexception One: * * 12:public static void Main (string[] args) throws IOException {://TODO auto-generated method stub 14:file filedir=new File ("/home/hado
Op/data/lucenedata ");
15:file[] Textfiles=filedir.listfiles (); 16:for (File file:textfiles) {17:system.out.println (File.getabsolutepath ()); 18:system.out.println (File.getCano
Nicalpath ());
19:system.out.println (File.getname ());
21:22:} 23:24:} 1:package filetest;
2:3: Import java.io.File;
4:import java.io.IOException; 5:6: public class Dirlist {7:8:/** 9: * @param args: * @throws ioexception One: * * 12:public static void Main (string[] args) throws IOException {://TODO auto-generated method stub 14:file filedir=new File ("/home/hado
Op/data/lucenedata ");
15:file[] Textfiles=filedir.listfiles (); 16:for (File file:textfiles) {17:system.out.println (File.getabsolutepath ()); 18:system.out.println (File.getCanonicalP
Ath ());
19:system.out.println (File.getname ()); 20:} 21:22:} 23:24:}
3,i/o
Read from standard input
Package IO;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
public class Echo {
/**
* @param args
* @throws ioexception
/public static void Main (string[) args) Throws IOException {
//TODO auto-generated method stub
bufferedreader stdin =new bufferedreader (New Inputstrea Mreader (system.in));
String s;
while ((S=stdin.readline ())!= null && s.length ()!=0) {
System.out.println (s);
}
}
}
Results
haha
haha
Output
Package IO;
Import Java.io.PrintWriter;
public class Changesystemout {
/**
* @param args */public
static void Main (string[] args) {
//TODO A uto-generated method Stub
printwriter out =new printwriter (system.out,true);
Out.println ("hello,world!");
}
Results
hello,world!