Jump to content




How to make new line?

help lua java

4 replies to this topic

#1 Pizza Fox

  • Members
  • 6 posts
  • Locationwhere am i

Posted 23 January 2015 - 02:45 AM

Hi, I made some code that takes a file copies it, pastes in new code at the top, and then adds in the old code. Right now I can add in 1 line of code since I don't know how to make a new line in a string. What I mean is how would does someone have a string that in the file is 1 line, but when written to a file is 2 lines. I believe it's something like /n which I tried in my emulator and it gets very angry:


Posted Image


The code for my program is here:
local newCode = "abc"
----------------------------
local f = fs.open(shell.getRunningProgram(), "r")--Retrieves files code. I used this program to test.
local oldFile = f.readAll()
f.close()
local f = fs.open(shell.getRunningProgram(), "w")--Writes over old code.
f.write(newCode)
f.close()
local f = fs.open(shell.getRunningProgram(), "a")--Adds original code to the newly added code
f.write(oldFile)
f.close()
----------------------------
The name of the program is filejump btw.


Anyways, thanks for reading and hopefully you can help me!

Edited by minecrafter1094, 23 January 2015 - 02:46 AM.


#2 Quintuple Agent

  • Members
  • 107 posts
  • LocationThis page

Posted 23 January 2015 - 03:12 AM

Well I just tested this out in minecraft itself and the code worked fine.

Now if you are wondering how to get a new line it is '\n'
When I tried your code I changed the newCode to equal abc\n and it created a new line, however you could also do fs.writeLine(newCode) which will write the text then append a new line.

I edited the code and tried it and this worked the way I believe you wanted it to work.

local newCode = "abc"
local editFile=shell.getRunningProgram()
----------------------------
local f = fs.open(editFile, "r")--Retrieves files code. I used this program to test.
local oldFile = f.readAll()
f.close()
local f = fs.open(editFile, "w")--Writes over old code.
f.writeLine(newCode)
f.write(oldFile)
f.close()
----------------------------


Also I don't know why you are getting a Java exception error, but I believe it is the emulator, because as I said it worked fine for me in minecraft.

Edited by Quintuple Agent, 23 January 2015 - 03:24 AM.


#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 23 January 2015 - 04:50 AM

View PostQuintuple Agent, on 23 January 2015 - 03:12 AM, said:

Also I don't know why you are getting a Java exception error, but I believe it is the emulator, because as I said it worked fine for me in minecraft.

Just a guess, but likely the emulator in concern is failing to drop the file handle on the script that's running at the correct time (ie, after loading it from the disk, but before actually executing it).

#4 Pizza Fox

  • Members
  • 6 posts
  • Locationwhere am i

Posted 23 January 2015 - 05:21 AM

Awesome! Your code looks cool and at least 1/4 smaller than before. As for the Java error I don't even really know, honestly I think I put the \n some weird way other so who knows

Anyways thanks for the help!

Edited by Pizza Fox, 13 September 2017 - 11:30 PM.


#5 Xtansia

  • Members
  • 492 posts
  • LocationNew Zealand

Posted 25 January 2015 - 03:27 AM

View PostBomb Bloke, on 23 January 2015 - 04:50 AM, said:

View PostQuintuple Agent, on 23 January 2015 - 03:12 AM, said:

Also I don't know why you are getting a Java exception error, but I believe it is the emulator, because as I said it worked fine for me in minecraft.

Just a guess, but likely the emulator in concern is failing to drop the file handle on the script that's running at the correct time (ie, after loading it from the disk, but before actually executing it).

CCEmuRedux uses ComputerCraft, and doesn't touch script handling, so if it was a lingering file handle it would be either CC or Java being dumb.


I've tried running code that edits the running program file in CCEmuRedux and it's worked every time with no exceptions.
If OP is able to replicate the issue, could you please post the output from running the emulator from the command line, when the exception occurs.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users