Wednesday, 27 September 2017

Java Files

Create a New File if it not exitst and write content into file

import java.io.*;

public class WriteIntoFile {

public static void main(String args[]) throws IOException{

  File f =new File("log.txt");
       
           if(!f.exists())
           f.createNewFile();
           System.out.println(f.getAbsolutePath());

       
         FileOutputStream fout = new FileOutputStream(f);
fout.write("Writing into file".getBytes());

fout.flush();
fout.close();

       
       
         System.out.println("success");
}
}



--------------------------------------------------------------------------------------------------------------

  Reading from file and writing into Console output



import java.io.*;

public class ReadingFromFile {

public static void main(String args[]) throws IOException{

      FileInputStream in = new FileInputStream("log.txt");
 
        int c;
        while((c=in.read())!=-1){
        System.out.print((char)c);
        }

}

}



--------------------------------------------------------------------------------------------------------------

Write and Read from File using  FileInputStream  &  FileOutputStream



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class ReadingWriting {

public static void main(String args[]) throws IOException{

Scanner sc = new Scanner(System.in);
System.out.print("enter input: ");
String str= sc.nextLine();
System.out.println(str);

FileOutputStream fout=new FileOutputStream("Output.txt");
//System.out.println(str.getBytes());

fout.write(str.getBytes());

fout.flush();
fout.close();


FileInputStream fin=new FileInputStream("Output.txt");
int c;
while((c=fin.read())!=-1){
System.out.print((char)c);
}

fin.close();
}
}


//enter input: a b c
//a b c
//9732983299                   output byte
// System.out.print((char)c);  output chars



--------------------------------------------------------------------------------------------------------------


 Reading & Writing into files using FileReader and FileWriter

import java.util.Scanner;
import java.io.*;
public class FileReaderWriter {

public static void main(String args[]) throws IOException{

Scanner sc = new Scanner(System.in);

System.out.print("enter input: ");
String str = sc.nextLine();

FileWriter fout = new FileWriter("fout.txt");


fout.write(str);

fout.flush();
fout.close();

int c;

FileReader fin = new FileReader("fout.txt");

while((c=fin.read())!=-1){
System.out.print((char)c);
}


fin.close();
}
}

--------------------------------------------------------------------------------------------------------------

InputStream & OutputStream 


import java.io.*;

public class InputOutputStream {

public static void main(String args[]) throws IOException{

OutputStream out=new FileOutputStream("file.txt");

byte bArr[]={65,32,66,32,67};

for(int i=0;i<bArr.length;i++){
out.write(bArr[i]);
}
out.flush();
out.close();



InputStream in = new FileInputStream("file.txt");

int size=in.available();
System.out.println(size);

for(int j=0;j<size;j++){
System.out.print((char)in.read());
}

in.close();

}

}


// 5
// A B C

--------------------------------------------------------------------------------------------------------------

Create a new Directory

package com.files;

import java.io.*;
public class CreateDirectory {

public static void main(String args[]){

String dirname ="Z:/Java/Files/tmp";
File f = new File(dirname);

boolean check;
check=f.mkdir();
System.out.println(check);     // return true if directory is created
}
}
--------------------------------------------------------------------------------------------------------------

List Directories


import java.io.*;

public class ListDirectories {

public static void main(String args[]){

File file =null;
String[] paths;


file =new File("Z:/Java/Files");

  paths=file.list();
 
  for(String path:paths){
  System.out.println(path);
  }
}
}

--------------------------------------------------------------------------------------------------------------






--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------

http://tutorials.jenkov.com
Notes

FileInputStream & FileOutputStream 

-   Both are Byte Stream

-   Reads and writes 8 bits at a time

-  FileOutputStream can be used create a new file if not exist

-  File f =new File("log.txt");     It won't create a new file


FileReader  & FileWriter

- Both are character streams

-  Reads and  writes 16 bit unicode

-  Reads and writes 2 Bytes at a time

- Internally uses FileInputStream and FileOutputStream

-  FileWriter can be used to create a new file if not exist









FileInputStream

This stream is used for reading data from the files. Objects can be created using the keyword new and there are several types of constructors available.
Following constructor takes a file name as a string to create an input stream object to read the file −
InputStream f = new FileInputStream("C:/java/hello");
Following constructor takes a file object to create an input stream object to read the file. First we create a file object using File() method as follows −
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
Once you have InputStream object in hand, then there is a list of helper methods which can be used to read to stream or to do other operations on the stream.

FileOutputStream

FileOutputStream is used to create a file and write data into it. The stream would create a file, if it doesn't already exist, before opening it for output.
Here are two constructors which can be used to create a FileOutputStream object.
Following constructor takes a file name as a string to create an input stream object to write the file −
OutputStream f = new FileOutputStream("C:/java/hello") 
Following constructor takes a file object to create an output stream object to write the file. First, we create a file object using File() method as follows −
File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
Once you have OutputStream object in hand, then there is a list of helper methods, which can be used to write to stream or to do other operations on the stream.








Migrate Jenkins Job to New Jenkins Server

How to Migrate a Jenkins Job to New Jenkins Server

 (COMMENTS)
We recently setup a new Jenkins build server for some iOS applications and I wanted to find a quick way to copy a couple Jobs from the old server to the new one.
Below are a few small options I found while working on the task.

Option 1: Copy jobs directory

One option (and seems to be the recommended one) is to just copy the jobs directory from the old server to the new one.
From the documentation Moving/copying/renaming jobs:
You can:
  1. Move a job from one installation of Jenkins to another by simply copying the corresponding job directory.
  2. Make a copy of an existing job by making a clone of a job directory by a different name.
  3. Rename an existing job by renaming a directory. Note that the if you change a job name you will need to change any other job that tries to call the renamed job.
Those operations can be done even when Jenkins is running. For changes like these to take effect, you have to click "reload config" to force Jenkins to reload configuration from the disk.
For me, I skipped this option because I was having a hard time finding where the jobs directory was on the old server. (Or just too lazy to find it, and I only had a couple jobs to copy over)

Option 2: Try one of the plugins out there

There are some Jenkins plugins out there that provide some job export options. Here are a couple...

Option 3: Use Jenkins CLI

This is what I used, which worked nicely for only the few jobs we had. If you have a large number of Jenkins jobs, you may consider the first aproach above.
  1. First download the Jenkins CLI jar.
  • You can do this from your jenkin's CLI page within your installed Jenkins instance.
jenkins CLI menu
  1. Next we can use the following command (pointing to the old server) to list the jobs.
java -jar jenkins-cli.jar -s http://<YourBuildServer>:<YourBuildServerPort>/ list-jobs
  1. Using one job from the list above, let's copy the xml of a job to the clipboard. (I'm using a Mac which is were pbcopy & pbpastecome from below)
java -jar jenkins-cli.jar -s http://<YourBuildServer>:<YourBuildServerPort>/ get-job "NAME_OF_JOB" | pbcopy
This uses the cli get-job "NAME_OF_JOB" command to print the job's xml to stdout, which we pipe to pbcopy on the Mac to load the configuration into the clipboard. You could of course pipe the output to a file like ... > job.xml
  1. If the above command placed a job's XML into the clipboard, you can use the below command to add it to the new server.
pbpaste | java -jar jenkins-cli.jar -s http://<YourBuildServer>:<YourBuildServerPort> create-job "NAME_OF_JOB"
This uses pbpaste to take what is in the clipboard, send it to stdin and pipe it to the Jenkins cli's create-job "NAME_OF_JOB"command.
Hope this helps...



Tuesday, 26 September 2017

Change permissions in Linux

chown tomcat : tomcat -R voices
ls -lhtr
chmod 777 -R voices
ls -lhtr

Sunday, 17 September 2017

Remove local commits that are made in local branch

Throw away local commits in git



git reset --hard <the sha1 hash>


Example:

  $ git reset --hard cdb645b705e55985bb08f4fdb9004224414c5219

This will reset the changes back to the git commit
To see the changes Use
$ git log

Edit host file in Mac

sudo nano /private/etc/hosts
to edit the hosts file within Terminal, Control-O to save, then hit enter