Java C - 1
6WU|PPDU
•
-DYDLRNODVVHUVRP
OlVHUE\WHVWU|PPDU
9LG OlVQLQJ RFK VNULYQLQJ DQYlQGV QnJRW VRP
Java C - 2
InputStream
NDOODV VWU|PPDU
•
(Q VWU|P lU HWW IO|GH DY GDWD VRP VWU|PPDU LQ
SequenceInputStream
FileInputStream
HOOHU XW IUnQ HWW SURJUDP
•
PipedInputStream
ByteArrayInputStream
0DQ VNLOMHU Sn LQVWU|PPDU RFK XWVWU|PPDU
–
FilterInputStream
(Q VWU|P KDU HQ NRSSOLQJ WLOO HQ ILO HQ HQKHW
HOOHU HQ NRPPXQLNDWLRQVI|UELQGHOVH RFK
DQYlQGV I|U DWW VNULYD HOOHU OlVD GDWD WLOO
DataInputStream
PushBackInputStream
GHQQD NRSSOLQJ
BufferedInputStream
-DYDLRNODVVHUVRP
VNULYHUE\WHVWU|PPDU
Java C - 3
(WWH[HPSHOPHGUHDG
RFKZULWH
Java C - 4
import java.io.*;
OutputStream
FileOutputStream
PipedOutputStream
ByteArrayOutputStream
final class Kopiera
{
public static void main(String[] argument)
{
try
{
FileInputStream orginal =
new FileInputStream(argument[0]);
FileOutputStream kopia =
new FileOutputStream(argument[1]);
int tecken = 0;
while ((tecken = original.read()) != -1)
kopia.write(tecken);
FilterOutputStream
orginal.close();
kopia.close();
DataOutputStream
}
catch (FileNotFoundException fel)
{
System.err.println("Kan inte hitta filen: "
+ argument[0]);
}
catch (IOException fel)
{
System.err.println("I/O-fel:"
+ fel.getMessage());
}
catch (ArrayIndexOutOfBoundsException fel)
{
System.err.println("Använd:
<java Kopiera frånFil tillFil>");
}
PrintStream
BufferedOutputStream
}
}
==> cat > orginalet.txt
Detta är en textfil
<ctrl-D>
==> java Kopiera orginalet.txt kopia.txt
==> cat kopia.txt
Detta är en textfil
==>
(QHIIHNWLYDUH
ILONRSLHULQJ
)|U
Java C - 5
DWW RSWLPHUD VNULYQLQJ RFK OlVQLQJ DY
VW|UUH ILOHU NDQ NODVVHUQD %XIIHUHG,QSXW6WUHDP
6NULYDRFKOlVDGDWDWLOO
RFKIUnQHQELQlUILO
'DWD,QSXW6WUHDP
Java C - 6
RFK 'DWD2XWSXW6WUHDP OnWHU HWW
VNULYD RFK OlVD SULPLWLYD GDWDW\SHU WLOO RFK IUnQ HQ ILO
RFK %XIIHUHG2XWSXW6WUHDP DQYlQGDV
'DWD
'HVVD
ODJUDV ELQlUW G Y V LQWH L WHFNHQIRUPDW Sn ILOHQ
NODVVHU DGPLQLVWUHUDU YDU VLQD EXIIHUWDU Vn DWW
IDNWLVND VNULYQLQJDU RFK OlVQLQJDU PRW GH I\VLVND
)RUPDWHW
HQKHWHUQD G Y V ILOHUQD J|UV L VW|UUH EORFN
NDQ OlVDV Sn HQ DQQDQ PDVNLQ
lU PDVNLQREHURHQGH (Q ILO VNULYHQ Sn HQ
import java.io.*;
import java.io.*;
final class Kopiera
{
public static void main(String[] argument)
{
try
{
BufferedInputStream orginal =
new BufferedInputStream(
new FileInputStream(argument[0]));
final class WriteRead
{
static void main(String[] argument) throws IOException
{
int helTal = 7;
double decimalTal = Math.PI;
DataOutputStream tillFil = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("tal.dat")));
tillFil.writeInt(helTal);
tillFil.writeDouble(decimalTal);
BufferedOutputStream kopia =
new BufferedOutputStream(
new FileOutputStream(argument[1]));
tillFil.close();
// Fortsättningen är lika som i föregående
// exempel
DataInputStream frånFil = new DataInputStream(
new BufferedInputStream(
new FileInputStream("tal.dat")));
System.out.println(frånFil.readInt());
System.out.println(frånFil.readDouble());
frånFil.close();
}
}
==>java WriteRead
7
3.141592653589793
==>jobb>cat tal.dat
@
!¹TD-•==>
(WWH[HPSHOSnODJULQJDY
REMHNWSnHQILOGHO
Java C - 7
2P HQ NODVV LPSOHPHQWHUDU JUlQVVQLWWHW
6HULDOL]DEOH Vn NDQ NODVVHQV REMHNW KDQWHUDV
DY -DYDV GDWDVWU|PPDU
(WWH[HPSHOSnODJULQJDYHWW
REMHNWWLOOHQILOGHO
import java.io.*;
public class Spara
{
static Person kalle;
static ObjectOutputStream tillFil;
public static void main(String[] arg) throws Exception
{
kalle = new Person("Kalle", "Röd");
tillFil = new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream("person.dat")));
import java.io.*;
public class Cykel implements Serializable
{
private String färg;
public Cykel(String färgen)
{
färg = färgen;
}
public String getFärg()
{
return färg;
}
}
import java.io.*;
tillFil.writeObject(kalle);
tillFil.close();
}
}
import java.io.*;
public class Skapa
{
static Person anonym;
static ObjectInputStream frånFil;
public static void main(String[] arg) throws Exception
{
frånFil = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream("person.dat")));
public class Person implements Serializable
{
private String namn;
private Cykel cykeln;
public Person(String namnet, String färgen)
{
namn = namnet;
cykeln = new Cykel(färgen);
}
public String toString()
{
return namn + " äger en " + cykeln.getFärg() + " cykel";
}
}
Java C - 8
anonym = (Person)frånFil.readObject();
frånFil.close();
System.out.println(anonym);
}
}
Utskrift
Kalle äger en Röd cykel
-DYDLRNODVVHUVRP
OlVHUWHFNHQVWU|PPDU
2%6
Java C - 9
-DYDLRNODVVHUVRP
VNULYHUWHFNHQVWU|PPDU
Reader
2%6
Writer
PipedReader
InputStreamReader
Java C - 10
OutputStreamWriter
BufferedWriter
CharArrayReader
FileReader
FileWriter
BufferedReader
FilterReader
PipedWriter
FilterWriter
CharArrayWriter
StringReader
LineNumberReader
PrintWriter
PushbackReader
StringWriter
, NODVVKHDUDUNLQ µ:ULWHUµ I|UHNRPPHU PRWVYDUDQGH PHWRGHU VRP
, NODVVKHDUDUNLQ µ5HDGHUµ I|UHNRPPHU PRWVYDUDQGH PHWRGHU VRP
L NODVVKHDUDUNLQ µ2XWSXW6WUHDPµ PHQ KHOD WHFNHQ VNULYV L VWlOOHW
L NODVVKHDUDUNLQ µ,QSXW6WUHDPµ PHQ KHOD WHFNHQ OlVHV L VWlOOHW
I|U E\WHV
I|U E\WHV
, NODVVHQ :ULWHU ILQQV W H[ HQ PHWRG µZULWHµ
, NODVVHQ UHDGHU ILQQV W H[ HQ PHWRG µUHDGµ
2P PDQ YLOO VNDSD HQ VWU|P VRP lU VNULYHU WLOO HQ WH[WILO Vn NDQ PDQ
2P PDQ YLOO VNDSD HQ VWU|P VRP OlVHU WHFNHQ IUnQ HQ WH[WILO Vn NDQ PDQ
VNDSD HWW REMHNW DY NODVVHQ µ)LOH:ULWHUµ
VNDSD HWW REMHNW DY NODVVHQ µ)LOH5HDGHUµ
(WW H[HPSHO PHG WHFNHQEDVHUDG Java C - 11
OlVQLQJ RFK VNULYQLQJ WLOO RFK IUnQ
ILO
(WW H[HPSHO PHG UDGYLV OlVQLQJ
RFK VNULYQLQJ WLOO RFK IUnQ ILO
Java C - 12
0HWRGHQ UHDG/LQH lU GHILQLHUDG L NODVVHQ %XIIHUHG5HDGHU
import java.io.*;
final class Kopiera
{
public static void main(String[] arg)
throws Exception
{
BufferedReader orginal = new BufferedReader
(new FileReader(argument[0]));
BufferedWriter kopia = new BufferedWriter
(new FileWriter(argument[1]));
5HDG/LQH OlVHU IUDP WLOO QlVWD WHFNHQ I|U Q\ UDG
0HWRGHUQD SULQW RFK SULQW/LQH lU GHILQLHUDGH L
NODVVHQ 3ULQW:ULWHU
0HWRGHUQD SULQW RFK SULQWOQ lU |YHUODJUDGH Vn DWW GH
NDQ DQURSDV PHG GH IOHVWD W\SHU DY YDULDEOHU VRP DUJXPHQW
int tecken = 0;
import java.io.*;
while ((tecken = orginal.read()) != -1)
kopia.write(tecken);
final class Kopiera
{
public static void main(String[] argument)
throws Exception
{
BufferedReader orginal =
new BufferedReader(
new FileReader(argument[0]));
orginal.close();
kopia.close();
}
}
PrintWriter kopia =
new PrintWriter(
new BufferedWriter(
new FileWriter(argument[1])));
String rad = null;
==> cat > orginalet.txt
Detta är en textfil
<ctrl-D>
==> java Kopiera orginalet.txt kopia.txt
==> cat kopia.txt
Detta är en textfil
==>
while ((rad = orginal.readLine()) != null)
kopia.println(rad);
orginal.close();
kopia.close();
}
}
Java C - 13
Java C - 14
.ODVVHQ6\VWHP
•
.ODVVHQ 6\VWHP L SDNHWHW
java.lang
,QOlVQLQJ
LQQHKnOOHU
•
WUH ROLND VWU|PPDU GHILQHUDGH VRP
'DWD VRP VNULYV Sn WDQJHQWERUGHW KDPQDU L
VWU|PPHQ 6\VWHPLQ
NODVVYDULDEOHU
•
System.in
System.out
System.err
•
)|U DWW OlVD WHFNHQ RFK LQWH E\WHV NRSSODV
VWU|PPHQ 6\VWHPLQ WLOO HWW REMHNW DY W\SHQ
,QSXW6WUHDP5HDGHU
System.in lU HQ
InputStream.
VWDWLVN UHIHUHQV
DY W\SHQ
•
/lVQLQJHQ EOLU HIIHNWLYDUH RP SURJUDPPHW OlVHU
LQ KHOD UDGHU DY WHFNHQ RFK LQWH WHFNHQ I|U
•
System.out
RFK
System.err
UHIHUHQVHU DY W\SHQ
•
%nGH
System.out
NRSSODGH PRW
lU VWDWLVND
WHFNHQ 'lUI|U NRSSODV VWU|PPHQ WLOO HWW REMHNW
PrintStream
System.err EUXNDU YDUD
VNlUPHQ PHGDQ System.in EUXNDU
DY W\SHQ %XIIHUHG5HDGHU
RFK
YDUD NRSSODG PRW WDQJHQWERUGHW
System.in
(InputStream) (InputStreamReader)
MinaInData
(BufferedReader)
BufferedReader MinaInData =
new BufferedReader(
new InputStreamReader(System.in));
•
, NODVVHQ %XIIHUHG5HDGHU ILQQV HQ PHWRGHQ
readLine
(WW H[HPSHO PHG UDGYLV OlVQLQJ
RFK VNULYQLQJ
1nJUDPHWRGHUL
NODVVHQ3ULQW6WUHDP
Java C - 15
, NODVVHQ 3ULQW6WUHDP I|UHNRPPHU PRWVYDUDQGH PHWRGHU
WLOO GH EnGD SULQW RFK SULQW/LQH VRP lU GHILQLHUDGH L
NODVVHQ 3ULQW:ULWHU
import java.io.*;
final class Eko
{
public static void main(String[] argument)
throws Exception
{
BufferedReader orginal =
new BufferedReader(
new InputStreamReader(System.in));
PrintStream kopia = System.out;
String rad = null;
while ((rad = orginal.readLine()) != null)
kopia.println(rad);
Java C - 16
.RQVWUXNWRUHU
PrintStream(OutputStream out),
PrintStream(OutputStream out, boolean autoFlush)
0HWRGHU VRP VNULYHU HWW REMHNW HOOHU YDULDEHO DY
DQJLYHQ W\S WLOO XWVWU|PPHQ
print(Object), print(String), print(char[]),
print(char), print(int), print(long), print(float),
print(double), print(bolean)
0HWRGHU VRP VNULYHU HWW REMHNW HOOHU YDULDEHO DY
DQJLYHQ W\S WLOO XWVWU|PPHQ RFK DYVOXWDU PHG HWW
Q\UDGVWHFNHQ
println(Object), println(String), println(char[]),
println(char), println(int), println(long),
println(float), println(double), println(bolean)
•
([HPSHO
System.out.print(”Vad heter du: ”);
System.out.flush();
orginal.close();
kopia.close();
}
}
•
6WUlQJHQ VNULYV LQWH XW GLUHNW XWDQ PHOODQODJUDV L
HQ XWGDWDEXIIHUW %XIIHUWHQ NDQ W|PPDV PHG KMlOS
DY PHWRGHQ
•
flush
2P VWU|PPHQ lU VNDSDG PHG µDXWR)OXVKµ
Vn IOXVKDU
println
EXIIHUWHQ DXWRPDWLVNW
WUXH
Java C - 17
(WW H[HPSHO VRP YLVDU LQOlVQQJ
DY WDO IUnQ WDQJHQWERUGHW
:UDSSHUNODVVHU
•
)|U YDUMH SULPLWLY GDWDW\S L -DYD I|UHNRPPHU GHW
Java C - 18
HQ RPVODJVNODVV ZUDSSHU
0HG KMlOS DY NODVVHQ 6WULQJ7RNHQL]HU Vn lU P|MOLJW
•
:UDSSHUNODVVHUQD ILQQV L SDNHWHW MDYDODQJ
EODQNWHFNHQ
•
, ZUDSSHUNODVVHQ I|UHNRPPHU GH YDQOLJDVWH
.ODVVHQ 6WULQJ7RNHQL]HU ILQQV L SDNHWHW MDYDXWLO
NRQVWDQWHUQD I|U GDWDW\SHQ W H[ NRQVWDQWHU I|U
DWW VHSUDUHUD VHNYHQVHU DY WHFNHQ nWVNLOGD DY
QHJDWLY RFK SRVLWLY RlQGOLJKHW
import java.io.*;
import java.util.*;
•
(Q ZUDSSHUNODVV NDQ DQYlQGDV RP HQNOD YDULDEOHU
VND OlJJDV LQ L GH I|UGHILQLHUDGH GDWDVWUXNWXUHU
VRP I|UHNRPPHU L -DYD
•
public final class Adderare
{
public static void main(String[] arg) throws Exception
{
BufferedReader tangentBord =
new BufferedReader(
new InputStreamReader(System.in));
)|U DWW RPYDQGOD HWW VWUlQJREMHNW VRP
System.out.print("Ge mig några tal: ");
System.out.flush();
UHSUHVHQWHUDU HWW WDO PnVWH HQ ZUDSSHUNODVV
DQYlQGDV
String rad = tangentBord.readLine();
StringTokenizer ordSeparator =
new StringTokenizer(rad, " \t");
string ord = "1056";
double summan = 0.0;
int heltalet = Integer.parseInt(ord);
while (ordSeparator.hasMoreTokens())
{
String ettOrd = ordSeparator.nextToken();
double ettTal = Double.parseDouble(ettOrd);
summan += ettTal;
}
System.out.println("Summan blir: " + summan);
}
}