Showing posts with label computer scienze. Show all posts
Showing posts with label computer scienze. Show all posts

Tuesday, 19 November 2013

Read or write from file inside a jar! What hell!

In my last "session" of java code I found myself in this problem: I must read and write from a file inside a jar. In detail i have a project that using this jar and from this project I must to interact with file.
Initially I thinked "oh it will very easy" but how wrong I was. The first problem is this:
How I can reference to file inside a jar? 
We must think that we cannot reference with an absolute or machine path: the project can be runned in hundreds of machines with hundreds of difference paths. So we must use a internal project path. Good. As first thing, read from this file. Java provides a fantastic method that which allows us to get an input stream from file with ponly passing as a parameter the name of file without the absolute path. For example: if my file inside jar has this name "pippo.txt" i can get an InputStream with this method:
getClass().getClassLoader().getResourceAsStream("pippo.txt")
To read the file,do this:
 InputStream stream = getClass().getClassLoader().getResourceAsStream("pippo.txt");
 String sCurrentLine;
BufferedReader br = new BufferedReader(new InputStreamReader(stream ));
          StringBuilder string = new StringBuilder();
while ((sCurrentLine = br.readLine()) != null) {
string .append(sCurrentLine);
}
string.toString();
Easy right? :)
But if I want to write inside a jar? This is a almost "impossible" task. You must think that a file inside a jar is as a file inside a .zip or .rar. Can you write a file inside a compress file? Yeah! But you must unzip the file, write the file and compressagain the entire package. In the same way works a file inside a jar. First you must extract all content of jar file, second you must write into file and third you must re-create the jar file and all of this operation you must do into your java code. It is an hell!
Usually the files inside a jar are a properties files: file that can be read and writed from other classes. But these files are special files: their are not a common files.  If you think that for your application there is a file that can be write as text file, you should think a way to create this file outside of jar. For example you can say to a class inside a jar to produce a file at runtime execution to  store in file system , save the path of file (you cannot save it on jar for the same problem described above: you cannot write inside a zip file.)and then the project that used this jar can ask to class to retrieve the path of this file and finally he can modify it.

Monday, 14 October 2013

My Android device as Remote Control!

Last night  I was watching Iron Man 3 on my computer, ehmmm no, my computer was connected on  tv and I was watching Iron Man on my tv. While I was watching my film, a whatsapp notification came out from my telphone!

DAMN! I MUST STOP THE FILM! I MUST TO GET  UP FROM MY CHAIR!

So I thought to myself "mmmmm probably there is some program to use my telephone as a remote control for VLC player on my computer" and then I found it! In the next few line I will explain as transforms you android device in a remote control.

First : your device and your computer must be connected at the same wi-fi connection. You must have vlc player on you computer, download it from http://www.videolan.org/vlc/ and you must download on your android device this program https://play.google.com/store/apps/details?id=org.peterbaldwin.client.android.vlcremote&hl=it . 


Now, after you have installed VLC on you computer, open it and go under Tools->Preferences. 
In the bottom right corner select "all" in show settings box. In " Interface" voice, click on "Main Interface" and select only "web" on "Extra Interface Modules". Click on triangle icon on "Main Interface" and click on Lua. Insert password on Lua http box. Save and restart VLC. 

Open a Windows Shell ( insert cmd on search program on windows) and insert ipconfig. Get your ip address,we will use after.If you have some problem to find your ip check the image.



From android device, open VLC remote and click on Add VLC Server. Now insert into dialog box ,the ip adress that you have find before and hit ok and insert the password into password dialog box that will open.
Well, now you can control your vlc from your CHAIR!

Monday, 7 October 2013

Export variable Java Linux! I see the light!

Why you don't find java or javac command in my terminal? I suppose  you have installed it, then because you don't find it? Maybe some malignant entities into your computer has removed java or maybe not.....where is the light in all of this?

It is simple! YOU MUST EXPORT JAVA_HOME! Java has an home? cool! 

Let's do it!

(This process set permantely your java variable into your computer)

  1. Open terminal in linux.
  2. If you don't know where is java is located, write in terminal: whereis java. A possible set of results can be:
    Save path that ending with ..../bin/java
  3. Now, write in your console: sudo nano /etc/profile and write at finsh of file these line:
    and instead of /home/lorenzo/Documenti/jdk1.7.0_25/bin insert the path, without the final words /java, that you have saved it before.
  4. Hit ctrl+o and press enter and hit ctrl+x.
  5. Write in console source /etc/profile 
  6. And work is complete!
FINISH HIM! (ehmm take out MortalKombat...)

Saturday, 5 October 2013

Damn! How to set image in a landscape mode into Lyx.

Ok. Finally i've discovered the magic formula to set a image in landscape mode into lyx, with custom margin.

The ingredients are these: 
  1. Modify Document preamble: go to Document->Settings->LaTextPreamble and add: 
    \usepackage{pdflscape}
    \usepackage{changepage}
    \usepackage{anysize}
  2. Before you insert the image, add a LaText box code (or hit CTRL+L) and insert: \marginsize{3cm}{2cm}{0cm}{1cm} % left rigth top bottom
    \begin{landscape}
  3.  Add Image (Insert->Image or Insert ->Floating Object and after, Insert->Image).
  4. Set image size: mouse right click on image and select Settings.
  5. After Image add another  LaText box code and insert: \end{landscape}
  6. Optionally: if you want change margin, add another time  \marginsize{3cm}{2cm}{0cm}{1cm} % left rigth top bottom with your preffered values.
This is the result: 

Friday, 27 September 2013

Lyx Program Listing cool style

Hello! While I was writing my thesis with lyx, I found myself to insert(to see to insert a box code, visit: http://stackoverflow.com/questions/2116944/insert-programming-code-in-a-lyx-document) a box contains a program code ( Java). Without options, the box code is very good, like:
It is nice, but if I want more style? In this document there are a lots of tricks to get our code box nicest http://ftp.uniroma2.it/TeX/macros/latex/contrib/listings/listings.pdf. To insert option configuration into box code, click with right click mouse into box code, and going to Settings->Advanced insert your options.

For example, to get this result : 






 


I have inserted these option: 

caption={Classe java Tripla}
emph={subject,predicate,object}
emphstyle={\color{blue}}
frame=shadowbox
keywordstyle={\color{violet}}
rulesepcolor={\color{gray}}

Try to believe!!

P.S. To enable all color into lyn, do this: go in Document->Settings->Latext Preamble and insert
\usepackage{xcolor}