Opening .java Files into a form.

Budda

Member
Joined
Dec 16, 2006
Messages
9
Programming Experience
Beginner
Ok. At the moment I have a form, with pretty much jack all working. I have basic things working, things that I have tought myself to do, burt there is one thing I am stuck on which I just can't figure out.

When pressing 'Ok' in the OpenFileDialog, I want the file, which is a .java file, to open into a RichTextBox, or a Text box, where I can edit the .java file, then save it using the SaveFileDialog.

Currently I have:
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OpenFile_FileOk([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.ComponentModel.CancelEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] OpenFile.FileOk
RichTextBox1.LoadFile(OpenFile.FileName)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
Which seems to be too short, which is why I think it doesn't work. When I try to use this, I choose the .java file, press Ok, and then it stops, and in the debugger it comes up with:
VB.NET:
File Format is not valid
I am guessing this is because it is a .java file, and I want to know how I can fix this so it opens it correctly. I know there is a way to open the .java files, because a friend of mine, whom I can't seem to contact, has done it before.

Thanks,
Budda

NOTE:
.java files layout is just normal writing. It can be opened and edited perfectly with notepad.
 
java files are plain text, so using a StreamReader would work here:
VB.NET:
Dim ofdOpen As New OpenFileDialog
With ofdOpen
  .Title = "Open file"
  .Filter = "Java SOurce Files (*.java)|*.java"
  .DefaultExt = "java"
  .FileName = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\*.java"
End With

If ofdOpen.ShowDialog <> DialogResult.Cancel Then
  Dim sr As New StreamReader(ofdOpen.FileName)
  RichTextBox1.Text = sr.ReadToEnd()
  sr.Close()
End If
 
So I just paste this into the code of the form?

Soprry, but I only started using VB properly yesterday, still trying to get the hang of most things.

EDIT: Copied it into the OpenFile Ok thingy, and at first it didn't work, so I changed a couple of things and now it works. Thanks.
Now onto saving =p
 
Last edited:
Problems saving .java file from RichTextBox

Opening the .java files is working fine now. Editing works perfectly. Saving works aswell, but my problem is that when I save, it saves the .java file in RTF. For example, instead of what a java file should be:
VB.NET:
public class stream {
 public stream() {
 }
 public stream(byte abyte0[]) {
  buffer = abyte0;
  currentOffset = 0;
 }
 public byte readSignedByteA() {
  return (byte)(buffer[currentOffset++] - 128);
 }
etc, it saves like:
VB.NET:
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Arial;}}
\viewkind4\uc1\pard\lang1033\fs17 public class stream \{\par
\par
\tab public stream() \{\par
\tab\}\par
\par
\tab public stream(byte abyte0[]) \{\par
\tab\tab buffer = abyte0;\par
\tab\tab currentOffset = 0;\par
\tab\}\par
\par
\tab public byte readSignedByteA() \{\par
\tab\tab return (byte)(buffer[currentOffset++] - 128);\par
\tab\}\par
\par
\tab public byte readSignedByteC() \{\par
\tab\tab return (byte)(-buffer[currentOffset++]);\par
\tab\}\par
Which will totally screw the .java file giving all sorts of errors when compiling.

Is there any way, to save the .java file NOT in RTF, so it doesn't have all those extra things such as font style, the "/tab" instead of spaces and the "/par" instead of enters.
 
There is a SaveFileDialog that works just the same as OpenFileDialog for getting a filename from user.
VB.NET:
[SIZE=2]RichTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText)[/SIZE]
[SIZE=2]RichTextBox1.SaveFile(filename, RichTextBoxStreamType.PlainText)[/SIZE]
By the way your code is C#, this is VB.Net only site.
 
Thanks. BTW, that first code was java. It was an example of how I want it to be set out when saving, and how it was set out.
 
Last edited:
By the way your code is C#, this is VB.Net only site.

actually his code is Java, he was posting a sample of the java code so we would know what the structure is


and here's how i would handle the reading and writing of the file:
VB.NET:
'Open the file
Dim ofdOpen As New OpenFileDialog
With ofdOpen
  .Title = "Open file"
  .Filter = "Java SOurce Files (*.java)|*.java"
  .DefaultExt = "java"
  .FileName = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\*.java"
End With

If ofdOpen.ShowDialog <> DialogResult.Cancel Then
  Dim sr As New StreamReader(ofdOpen.FileName)
  RichTextBox1.Text = sr.ReadToEnd()
  sr.Close()
End If

'Save the file
Dim sfdSave As New SaveFileDialog
With sfdSave
  .Title = "Open file"
  .Filter = "Java SOurce Files (*.java)|*.java"
  .DefaultExt = "java"
  .FileName = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\*.java"
End With

If sfdSave.ShowDialog <> DialogResult.Cancel Then
  Dim sw As New StreamWriter(sfdSave.FileName)
  sw.Write(RichTextBox1.Text)
  sw.Close()
End If
 
Back
Top