vaprosche. HELP ME!
3 отговора
100 прегледа
vaprosche. HELP ME!
ot skoro se zanimavam s JAVA. Dosega pisah na C++. Vaprosa mi e kak sas JAVA da otvori edin obiknoven .txt fail i da pisha v nego. Problema e, che kogato otvoria fail s tazi programka:
public class FileWrite{
public String s1;
public String outString;
public FileWrite(String outString){
this.outString=outString;
}
public void writenow(String myFile){
try {
BufferedReader in1 = new BufferedReader(new StringReader(outString));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(myFile)));
while ((s1=in1.readLine())!=null)
out.println(s1 + "/");
out.close();
} catch(EOFException e)
{
System.err.println("MY ERROR!!!! End of file");
}
catch (IOException e1)
{
System.err.println("MY ERROR!!!! IOException");
}
}
}
parvo se iztriva vsichko ot txt faila, i sled tova se zapisva novata infornacia.
V C++ moge da se otvori fail, da se pozicionira markera v kraia my i sled tova da se pishe v nego, dez da se gubi predishnoto my sadarganie.
KAK STAVA TOVA NA JAVA????
Ste sam mnogo blagodaren ako niakoi mi pomogne!


public class FileWrite{
public String s1;
public String outString;
public FileWrite(String outString){
this.outString=outString;
}
public void writenow(String myFile){
try {
BufferedReader in1 = new BufferedReader(new StringReader(outString));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(myFile)));
while ((s1=in1.readLine())!=null)
out.println(s1 + "/");
out.close();
} catch(EOFException e)
{
System.err.println("MY ERROR!!!! End of file");
}
catch (IOException e1)
{
System.err.println("MY ERROR!!!! IOException");
}
}
}
parvo se iztriva vsichko ot txt faila, i sled tova se zapisva novata infornacia.
V C++ moge da se otvori fail, da se pozicionira markera v kraia my i sled tova da se pishe v nego, dez da se gubi predishnoto my sadarganie.
KAK STAVA TOVA NA JAVA????
Ste sam mnogo blagodaren ako niakoi mi pomogne!
Re: vaprosche. HELP ME!
import java.io.*;
public class test {
RandomAccessFile file = null;
public test( String fileName ) throws IOException {
file = new RandomAccessFile( fileName, "rw" );
}
public void append( String s ) throws IOException {
file.seek( file.length() );
write( s );
}
public void write( String s ) throws IOException {
file.write( s.getBytes() );
}
public void close() throws IOException {
file.close();
}
public static void main( String args[] ) throws Exception {
test t = new test( "proba.txt" );
t.append( "hello\n" );
t.append( "world.\n" );
}
}
public class test {
RandomAccessFile file = null;
public test( String fileName ) throws IOException {
file = new RandomAccessFile( fileName, "rw" );
}
public void append( String s ) throws IOException {
file.seek( file.length() );
write( s );
}
public void write( String s ) throws IOException {
file.write( s.getBytes() );
}
public void close() throws IOException {
file.close();
}
public static void main( String args[] ) throws Exception {
test t = new test( "proba.txt" );
t.append( "hello\n" );
t.append( "world.\n" );
}
}
Re: vaprosche. HELP ME!
ia kato si now w Java gledaj tuka : [линк]
pone na men mnogo mi pomogna.
pone na men mnogo mi pomogna.